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.
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
else
// 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
System.debuglog(t)
Var shortTime As String = DateTime.Now.ToString(New Locale("en-US"), _ // just a sample
DateTime.FormatStyles.None, _
DateTime.FormatStyles.Short)
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")
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)
End
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
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
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")