I have a string which contains: 14 Dec 2011 9:45 am (take note that "AM" is not capitalized")
now I wanted to create a datetime variable with it.
I tried looking at this, but it is the opposite of what I wanted. I also tried this, but failed. What I've tried so far is this:
Dim dateNow As DateTime
Dim out As String = "14 Dec 2011 9:45 am"
dateNow = DateTime.ParseExact(out, "d MMM yyyy HH:mm tt", Nothing)
But sadly it's not working. Any idea? Thanks. VB.net Or C# code will be okay..
Your probably need single H instead of HH as the hour is single digit in the datetime string. You should have 09 has hour if you have HH for hour. Also use small h for 12 hours with AM and PM, Capital H is for 24 hours time format, like 1:28 PM would be 13:28 PM
dateNow = DateTime.ParseExact(out, "d MMM yyyy h:mm tt", Nothing)
The description abut using different options for hour shown below.

You can learn more about custom formats for DataTime here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With