11

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!

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
eastboundr
  • 1,857
  • 8
  • 28
  • 46

8 Answers8

18

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")
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
3

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)

TimWolla
  • 31,849
  • 8
  • 63
  • 96
  • Make sure to use proper formatting (backticks for inlinecode), as it makes your answer more readable. Have a look at the [help](http://stackoverflow.com/help) and test out the editor toolbar so see what is possible. – TimWolla Jun 21 '13 at 01:02
2
Label1.Text = Now.ToShortTimeString.ToString()   (10:26 PM)

Label1.Text = Now.ToLongTimeString.ToString()    (10:26:30 PM) 
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
1
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

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @Cyborgx37: That's the misleading part of this question. Actually OP has no problem with parsing 1400 to a DateTime: _"...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"_ He just wants to convert a DateTime variable to a String with the proper format. – Tim Schmelter Jun 12 '12 at 21:14
0

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

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
0

Try This One...

  Dim TimeNow As String
  TimeNow = TimeOfDay.ToString("h:mm:ss tt")
Edwin Thomas
  • 1,186
  • 2
  • 18
  • 31
0

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

-1
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#

  • This solution doesn't really clarify anything, while it is a direct response to the question title, it does not involve the code posted in the question and it does not follow or reference the standard guidance on this topic from the MS Docs. You have also introduced C# even though this is tagged _and_ titled specifically with VB.net. Please try not to open up old posts without making a genuine effort to contribute to the community and including strong justification on why this answer is superior to the existing ones. – Chris Schaller Feb 08 '22 at 08:12