I have this datetime string like “11:45am Mar 20, Wed 20, Wed”. How can parse it?
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)
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?
If you would like to convert the string “01012019” to a date format, you can can assign type Datetime
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
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?
Please try this.
DateTime dt= dateTime.ParseExact("your_string.trim", "hh:mmtt MMM dd, ddd dd, ddd", System.Globalization.CultureInfo.InvariantCulture)