### Parse Date & Time Subs

`Subs to parse date & time for a given format.
Examples using the Subs
Dim dtt As Long = DateTimeToTicks("1958-08-09 09:08:00", "yyyy-MM-dd HH:mm:ss")
Log($"DateTimeToTicks: $DateTime{dtt}"$)
Log($"DateFromTicks: ${DateFromTicks(dtt, "dd/MM/yyyy")}"$)
Log($"TimeFromTicks: ${TimeFromTicks(dtt, "hh:mm:ss a")}"$)
Output:
DateTimeToTicks: 1958-08-09 09:08:00
DateFromTicks: 09/08/1958
TimeFromTicks: 09:08:00 AM
Subs
' Parse datetime to ticks for a given format
' Example:
' Dim dtt As Long = DateTimeToTicks("1958-08-09 09:08:00", "yyyy-MM-dd HH:mm:ss")
`Sub DateTimeToTicks(dts As String, dtformat As String) As Long
' Split the datetime format into date & time format
Dim s() As String = Regex.Split(" ", dtformat)
DateTime.DateFormat = s(0)
DateTime.TimeFormat = s(1)
' Split the dateetime string in date & time string
Dim dt() As String = Regex.Split(" ", dts)
' Parse the date & time using datetimeparse with seperate date & time strings
Dim ticks As Long = DateTime.DateTimeParse(dt(0), dt(1))
Return ticks
End Sub`
' Get the date from ticks in format
' Example:
' Dim dtt As Long = DateTimeToTicks("1958-08-09 09:08:00", "yyyy-MM-dd HH:mm:ss")
' DateFromTicks(dtt, "dd/MM/yyyy")
`Sub DateFromTicks(dl As Long, dformat As String) As String
DateTime.DateFormat = dformat
Dim DateString As String = DateTime.Date(dl)
Return DateString
End Sub`
' Get the time from ticks in format
' Example:
' Dim dtt As Long = DateTimeToTicks("1958-08-09 09:08:00", "yyyy-MM-dd HH:mm:ss")
' TimeFromTicks(dtt, "hh:mm:ss a")
`Sub TimeFromTicks(tl As Long, tformat As String) As String
DateTime.TimeFormat = tformat
Dim TimeString As String = DateTime.Time(tl)
Return TimeString
End Sub`
`

Changed: , Category: DateTime, File: T6C183M1332.htm, ID: 1332