Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a 24 hour time to 12 hour in VB.net as hh:mm AM/PM

So let's say I have 1400, I want to convert it into 2:00PM

I tried the following:

Dim convertedTime As String = DateTime.ParseExact(theTime,"HHmm", Nothing)

And it would give me this:

6/12/2012 02:00:00 PM

I do not want the date part, neither do I need the seconds. All I need is 2:00PM

How could I achieve this? Thanks!

like image 684
eastboundr Avatar asked Jun 12 '12 20:06

eastboundr


People also ask

How do I convert to 12 hours?

Converting from 24 hour to 12 hour clock Starting from the first hour of the day (0:00 / midnight to 0:59), add 12 hours and AM to the time: 0:30 = 12:30 AM. 0:55 = 12:55 AM.


1 Answers

The ParseExact method returns a DateTime value, not a string. If you assign it to a string variable you will be converting it automatically, which uses the standard formatting.

If you want it in a specific format, then format the DateTime value as a string:

Dim d As DateTime = DateTime.ParseExact(theTime,"HHmm", Nothing);
Dim convertedTime As String = d.ToString("hh:mm tt")
like image 108
Guffa Avatar answered Sep 21 '22 16:09

Guffa