Search code examples
c#datetimeformattingdatetime-formattypeconverter

Convert to DateTime Format


Hello is there a way to convert this string "Saturday 04/23/2013 11:05 PM" to a valid DateTime Format?

Because it gives me FormatExceptionError everytime I execute this condition:

String was not recognized as a valid DateTime because the day of week was incorrect.

if(DateTime.Parse("Saturday 04/23/2013 11:05 PM") < DateTime.Today)
{
//code here
}

Is there a solution to this problem?


Solution

  • use DateTime.ParseExact()

    string _strdate = "Tuesday 04/23/2013 11:05 PM"; // should be tuesday
    DateTime _date = DateTime.ParseExact(_strdate,"dddd MM/dd/yyyy hh:mm tt", 
                                         CultureInfo.InvariantCulture)
    

    enter image description here