Help with date parser

I have this datetime string like “11:45am Mar 20, Wed 20, Wed”. How can parse it?

Hi @lancer,
Use the below code

DateTime dtime= DateTime.ParseExact("1:45 am Mar 20, Wed 20, Wed", "h:mm tt MMM dd, ddd dd, ddd", System.Globalization.CultureInfo.InvariantCulture)

Regards,
Arivu :slight_smile:

Hey arivus,

How would i get a string to Parse as a date? eg: InvoiceDate (genericValue) is 01012019. Converted to 01-01-2019 ? (Can this be done as GenericValue variable, or does something need to be changed to DateTime variable?

Thanks

Hi @MikeBlades,

If you would like to convert the string “01012019” to a date format, you can can assign type Datetime dt=datetime.ParseExact("01012019","ddMMyyyy",system.Globalization.CultureInfo.InvariantCulture)
If you just need an output string as “01-01-2019” and no further operations needs to carryout with the date format, you can use string type strdate=datetime.ParseExact("01012019","ddMMyyyy",system.Globalization.CultureInfo.InvariantCulture).ToString("dd-MM-yyyy")

Warm regards,
Nimin

1 Like

Thanks for answering! Got this error:
String was not recognized as a valid DateTime.
The string was scrapped from the web and it’s in the form of “01:45am” instead of “1:45 am”. Also, sometimes the time could have some errors in it, for example, “12:45pm” won’t be recognized. Anything I can do?

Hi @lancer,

Please try this.
DateTime dt= dateTime.ParseExact("your_string.trim", "hh:mmtt MMM dd, ddd dd, ddd", System.Globalization.CultureInfo.InvariantCulture)

Regards,
Nimin