'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!
Solution 1:[1]
Label1.Text = Format(Now, "hh:mm"): Label1's text= 10:26 (or whatever the time is)
Label1.Text = Format(Now, "hh:mm tt"): Label's text = 10:26 PM
Label1.Text = Format(Now, "dddd dd, MMMM, YYYY"): Label1's text = Thursday 21, August, 2014 (or whatever the date is)
Solution 2:[2]
Label1.Text = Now.ToShortTimeString.ToString() (10:26 PM)
Label1.Text = Now.ToLongTimeString.ToString() (10:26:30 PM)
Solution 3:[3]
Dim theTime = New Date(2012, 6, 12, 14, 0, 0)
Dim formatted = theTime.ToString("h:mm tt", Globalization.CultureInfo.InvariantCulture)
Solution 4:[4]
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
Solution 5:[5]
Try This One...
Dim TimeNow As String
TimeNow = TimeOfDay.ToString("h:mm:ss tt")
Solution 6:[6]
You Can Use String.Format() Function Without ParseExtract() Function
VB.NET:-
Dim time As String = String.Format("{0:hh}:{0:mm} {0:tt}", Date.Now) '12:00 PM'
Dim time_1 As String = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", Date.Now) '12:00:25 PM'
Dim time_2 As String = String.Format("{0:mm}:{0:ss}.{0:fff}", Date.Now) '00:25.986 - Mostly Used In Stopwatches'
CSharp (C#):-
String time = String.Format("{0:hh}:{0:mm} {0:tt}", DateTime.Now); // 12:00 PM
String time_1 = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", DateTime.Now); // 12:00:25 PM
String time_2 = String.Format("{0:mm}:{0:ss}.{0:fff}", DateTime.Now); // 00:25.986 - Mostly Used In Stopwatches
Above Codes Are Examples, You Can Use Above Codes To Experiment/Observe/Make Applications With It
Solution 7:[7]
stackoverflowuser:
You Can Use String.Format() Function Without ParseExtract() Function
VB.NET:-
Dim time As String = String.Format("{0:hh}:{0:mm} {0:tt}", Date.Now) '12:00 PM'
Dim time_1 As String = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", Date.Now) '12:00:25 PM'
Dim time_2 As String = String.Format("{0:mm}:{0:ss}.{0:fff}", Date.Now) '00:25.986 - Mostly Used In Stopwatches'
CSharp (C#):-
String time = String.Format("{0:hh}:{0:mm} {0:tt}", DateTime.Now); // 12:00 PM
String time_1 = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", DateTime.Now); // 12:00:25 PM
String time_2 = String.Format("{0:mm}:{0:ss}.{0:fff}", DateTime.Now); // 00:25.986 - Mostly Used In Stopwatches
Above Codes Are Examples, You Can Use Above Codes To Experiment/Observe/Make Applications With It
You Can Use Also In:
VB.NET:-
Label1.Text = String.Format("{0:hh}:{0:mm} {0:tt}", Date.Now) 'Display: 12:00 PM'
CSharp (C#):-
label1.Text = SString.Format("{0:hh}:{0:mm} {0:tt}", DateTime.Now); // Display: 12:00 PM
You Will Get The Formatted Timings In Any Label In Both VB.NET And C#
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TimWolla |
| Solution 2 | Kamil Budziewski |
| Solution 3 | Tim Schmelter |
| Solution 4 | |
| Solution 5 | Edwin Thomas |
| Solution 6 | stackoverflowuser |
| Solution 7 | stackoverflowuser |
