HOW TO PARSE OUT DATE TIME FROM A STRING AND MATCH WITH A GIVEN FORMAT

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
C# 
C#
 1272  View(s)
Ratings:
Rate this:

My datetime value is stored in the column as 12/03/2013 12:00 am, I am trying to parse out the date from a different column which is a string so i check to see if the datetime is a valid date. I can not figure out the format, yyyy-mm-dd hh:mm tt doesnt seem to make the condition true.

string[] format = new string[] { "yyyy-mm-dd hh:mm tt" };

DateTime datetime;

//12/03/2013 12:00 am

if (DateTime.TryParseExact(userFilters.FilterValue, format, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out datetime))

{

  DateEntered = datetime;

}

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: how to parse out date time from a string and match with a given format

    Hi Ashish,

    You can try this :

    string[] format = new string[] { "MM/dd/yyyy hh:mm tt"
    };
    DateTime datetime;
    //12/03/2013 12:00 am  if (DateTime.TryPars eExact(userFilters.FilterValue, format, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out datetime))
    {
     DateEntered = datetime;
    }

      Modified On Apr-03-2018 06:25:07 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!