How to change shorttime into military time?

Hello all,

I save a time using ShortTime, so it appears as 6:00PM. How can I convert that to 1800?

At this point I can change the storage format of the time too if that simplifies this. I did try using SQLdatetime, but I don’t need or want the date, only the time in military format.

Ideas anyone?

Add 12 if it’s PM

var d As New Date
var timeInShortFormat As String
timeInShortFormat = d.ShortTime

timeInShortFormat = timeInShortFormat.Replace(":", "")
if (timeInShortFormat.Right(2) = "PM") then
  // add 12 to the hour, will need to convert from a string to an integer
  // format the string as you see fit
end if
Dim SQLDateTime As String = "2023-05-23 22:00"
Dim dt As datetime = datetime.FromString(SQLDateTime)
Dim t As String = dt.ToString("HH:mm:ss") 'or HHmm if you want it as 1800

When I’m storing just the time, I store it in seconds. It’s easy to convert to/from and then convert it for display.

1 Like

Thank you everyone!
I appreciate the tips and sample code.


Whats the size of the integer?

Var shortTime As String = DateTime.Now.ToString(New Locale("en-US"), _  // just a sample
                            DateTime.FormatStyles.None, _

shortTime = shortTime.Trim // no spaces around

Var shortTimeInSeconds As Integer = shortTime.Val * 3600 + _
                                      shortTime.NthField(":",2).Val * 60 + _
                                      If(shortTime.Right(2)="PM", 43200, 0)

Var militaryTime As String = Ctype(shortTimeInSeconds/3600,Integer).toString("00")+":"+_
                               Ctype(shortTimeInSeconds mod 3600 / 60,Integer).toString("00")

Since the max value is 86400, anything bigger than 16-bit will do. I just use an int in the database. An extra byte is no biggie.

I see… you do not use total seconds - as in what the Date object has - you use total seconds for the day.
That’s where my confusion came in.

Thanks everyone!

If you save the date/time you could use this extension method.

Public Function MilitaryTime(Extends Value As DateTime) As String
  Var t As Integer = Value.Hour * 100 + Value.Minute
  Return t.ToString("0000")
End Function

Sorry, I did not remove the colon converting your string, again, now avoiding conversion of already converted strings:

// Convert time strings to military time

Var shortTime As String = "11:37PM"
//Var shortTime As String = "23:37"
//Var shortTime As String = "2337"

shortTime = shortTime.Trim // assure no spaces around

Var shortTimeInSeconds As Integer // useful intermediary value

If shortTime.Length = 4 Then // already military
  shortTimeInSeconds  = shortTime.Left(2).Val * 3600 + _
                        shortTime.Right(2).Val * 60
Else // Need convertion
  shortTimeInSeconds  = shortTime.Val * 3600 + _
                        shortTime.NthField(":",2).Val * 60 + _
                        If(shortTime.Right(2)="PM", 43200, 0)

Var militaryTime As String = Ctype(shortTimeInSeconds/3600,Integer).toString("00")+_
                    Ctype(shortTimeInSeconds mod 3600 / 60,Integer).toString("00")

// Return (militaryTime : shortTimeInSeconds  ) // you can return a pair in a method

Sorry, I misread your post:

Dim shortTime As String = "6:00PM"
Dim milTime As String = datetime.FromString(shortTime.ReplaceAll("A"," A").ReplaceAll("P"," P"), New Locale("en-US")).ToString("HHmm")

or a more readable version :wink:

Dim shortTime As String = "6:00PM"
Dim longTime As String = shortTime.ReplaceAll("A"," A").ReplaceAll("P"," P")
Dim dt As datetime = datetime.FromString(longTime, New Locale("en-US"))
Dim milTime As String = dt.ToString("HHmm")