Parse date string using DateTime.Parse() & ParseExact()

Asp.net mvc DateTime.Parse() & DateTime.ParseExact() to parse datetime strings in DotNet. And parsing date using different formats like: "dd/MM/yyyy".

 DateTime   
 

Posted: October 27, 2018. | By: mustafa

DateTime format in Asp.Net c#, how to handle different datetime formats and their conversions. Also we need to convert from datetime to string and string to datetime while saving and retrieving dates from user input and databases. 

Datetime methods available in DotNet

  • Convert.ToDateTime()
  • DateTime.Parse()
  • DateTime.ParseExact()
  • DateTime.TryParse() and
  • DateTime.TryParseExact()

Datetime is a struct type which can be represented in a formatted string. Today we are going to discuss the two methods DateTime.Parse() & DateTime.ParseExact(). And string formatting of date object

DateTime.Parse() is more simple method to convert string date time representation to DateTime object. However, DateTime.ParseExact() is more strict and it try to comply with the specified date format and culture.


DateTime.Parse()

DateTime.Parse(String value)

Value: It is string representation of date and time. For instance, DateTime.Parse(“01/10/2018”);

If value is null then it returns ArgumentNullException and if value contains some invalid date format then it returns FormatException.


DateTime.ParseExact()

It converts specified string to equivalent DateTime with specified format and culture. The string value of format must match with string value of datetime.

DateTime.ParseExact(salesOrder.OrderDate, "dd-MM-yyyy", CultureInfo.InvariantCulture)

DateTime.TryParse()

It converts specified string data to equivalent datetime and returns Boolean value after parsing which indicates parsing is succeeded. It returns MinValue(1/1/0001 12:00:00 AM) if datetime conversion is failed.

And

DateTime.TryParseExact()

It converts specified string to equivalent DateTime with specified format and culture. The string value of format must match with string value of datetime.
It throws exception only if DateTimeStyle value is not valid otherwise returns MinValue. Secondly, it always returns a Boolean value (true or false).


While we need to compare datetime object with user input date string we need to convert date from string.

Let us take and example where we have the user input from the datetime picker in jquery.
StartOrderDate is 01-10-2018 or 1 Oct 2018 and format "dd-MM-yyyy".

DateTime.ParseExact() we have used the format "dd-MM-yyyy" above.

SQLDatetime.png, DateTime, Asp.Net, DateTime Parse, Date, Date Conversions,

And SqlDatetime we have in the database is '2018-05-10 11:50:15.123' having the datetime format as "yyyy-MM-dd HH:mm:ss.fff".

If we use the format "dd-MM-yyyy" we will get the datetime format exception: String was not recognized as a valid DateTime.

Next we provide datetime format as "yyyy-MM-dd HH:mm:ss.fff" for the method DateTime.ParseExact() we are able to convert datetime object.

Now we can compare both the dates to get the required result.

DateTime.ParseExact(salesOrder.OrderDate, "dd-MM-yyyy", CultureInfo.InvariantCulture) <= DateTime.ParseExact(DateTime.Now.ToShortDateString(), "MM/dd/yyyy", CultureInfo.InvariantCulture)

Note: We can use DateTime.Parse() to compare the dates easily.

Next we are going to use multiple formats to parse date string value.

string[] formats = { "dd-MMM-yyyy", "dd/MMM/yyyy", "dd-MM-yyyy", "dd/MM/yyyy", "dd-MM-yyyy HH:mm:ss" };

Here we can add required formats in string array above which we can use to parse date successfully.

Example:

DateTime.ParseExact(salesOrder.OrderDate, formats, null, DateTimeStyles.None)

Hope this article helps for parsing datetime and date strings.


related posts

Back to top