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

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

I tried the following:

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

And this will give me this:

12/12/2012 02:00:00 PM

I do not need a date, I also need seconds. All I Need is 2:00 PM

How could I achieve this? Thanks!

+6
source share
6 answers

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

If you want it in a specific format, 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") 
+10
source
 Dim theTime = New Date(2012, 6, 12, 14, 0, 0) Dim formatted = theTime.ToString("h:mm tt", Globalization.CultureInfo.InvariantCulture) 

Custom Date and Time Format Strings

+1
source

Label1.Text = Format(Now, "hh:mm") : Label1 text = 10:26 (or regardless of time)

Label1.Text = Format(Now, "hh:mm tt") : Label text = 10:26 PM

Label1.Text = Format(Now, "dddd dd, MMMM, YYYY") : Label1 text = Thursday 21, August 2014 (or whatever the date)

+1
source
 Label1.Text = Now.ToShortTimeString.ToString() (10:26 PM) Label1.Text = Now.ToLongTimeString.ToString() (10:26:30 PM) 
+1
source

There are two ways to achieve this.

Option 1 (using standard date and time format strings ):

 Dim theTime As DateTime = new DateTime(2008, 4, 10, 6, 30, 0) Dim convertedTime As String = theTime.ToString("t", CultureInfo.CreateSpecificCulture("en-us")) 

Option 2 (using custom date and time format strings ):

 Dim theTime As DateTime = new DateTime(2008, 4, 10, 6, 30, 0) Dim convertedTime As String = theTime.ToString("hh:mm tt") 

In both cases, convertedTime will be 6:30 AM

0
source

Try this one ...

  Dim TimeNow As String TimeNow = TimeOfDay.ToString("h:mm:ss tt") 
0
source

Source: https://habr.com/ru/post/917914/


All Articles