Sometimes you end up dealing with date values which are in the form of “YYYYMMDDHHMMSS:00000-GMT” (where “GMT” is a multiple of hours * 60 deviation from Greenwich Mean Time). Formatting those into usable date values can be done several ways. This is just one way. And I threw in a somewhat not-so-obvious KiXtart version of this function for the hell of it.
VBScript version
Function mDate(strDate, format)
Dim tmp, retval
On Error Resume Next
Select Case Lcase(format)
Case "short":
tmp = Mid(strDate, 5, 2) & "/" & _
Mid(strDate, 7, 2) & "/" & _
Left(strDate,4)
retval = FormatDateTime(tmp, vbShortDate)
Case "datetime":
tmp = Mid(strDate, 5, 2) & "/" & _
Mid(strDate, 7, 2) & "/" & _
Left(strDate,4) & " " & _
Mid(strDate, 9, 2) & ":" & _
Mid(strDate, 11, 2) & ":" & _
Mid(strDate, 13, 2)
retval = FormatDateTime(tmp, vbShortDate) & " " & _
FormatDateTime(tmp, vbLongTime)
Case "long":
tmp = Mid(strDate, 5, 2) & "/" & _
Mid(strDate, 7, 2) & "/" & _
Left(strDate,4) & " " & _
Mid(strDate, 9, 2) & ":" & _
Mid(strDate, 11, 2) & ":" & _
Mid(strDate, 13, 2)
retval = FormatDateTime(tmp, vbLongDate)
End Select
mDate = retval
End Function
wscript.echo mDate("20100305132705.00000-520", "short")
wscript.echo mDate("20100305132705.00000-520", "long")
wscript.echo mDate("20100305132705.00000-520", "datetime")
KiXtart Version
Function FormatDateTime($v, $f)
Dim $sc, $result
$sc = CreateObject("ScriptControl")
$sc.Language = "vbscript"
$result = $sc.Eval('FormatDateTime("'+$v+'",'+$f+')')
$sc = 0
$FormatDateTime = $result
EndFunction
Function mDate($strDate, $format)
Dim $tmp, $retval
Select
Case $format = "short"
$tmp = Substr($strDate, 5, 2) + "/" +
Substr($strDate, 7, 2) + "/" +
Left($strDate,4)
$retval = $tmp
Case Lcase($format) = "datetime"
$tmp = Substr($strDate, 5, 2) + "/" +
Substr($strDate, 7, 2) + "/" +
Left($strDate,4) + " " +
Substr($strDate, 9, 2) + ":" +
Substr($strDate, 11, 2) + ":" +
Substr($strDate, 13, 2)
$retval = $tmp
Case Lcase($format) = "long"
$tmp = Substr($strDate, 5, 2) + "/" +
Substr($strDate, 7, 2) + "/" +
Left($strDate,4)
$tmp = FormatDateTime($tmp, "vbLongDate")
$retval = $tmp
EndSelect
$mDate = $retval
EndFunction
? mDate("20100305132705.00000-520", "short")
? mDate("20100305132705.00000-520", "long")
? mDate("20100305132705.00000-520", "datetime")
Yes, the KiXtart version is somewhat “cheating” but who cares. It works, and it saves me a little bit of coding. The performance overhead delta is near-zero, unless you’re running on 1990’s hardware.
No comments:
Post a Comment