Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime Custom format - Show AM/PM as capitalized and not am / pm

Tags:

string

c#

vb.net

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..

like image 799
Codemunkeee Avatar asked Oct 28 '25 14:10

Codemunkeee


1 Answers

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.

enter image description here

You can learn more about custom formats for DataTime here.

like image 150
Adil Avatar answered Oct 30 '25 03:10

Adil