Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert WMI datetime to VBScript datetime

18 views
Skip to first unread message

Ali AYEN

unread,
Feb 11, 2004, 8:41:54 AM2/11/04
to
I got a datetime value like 20040211140716.500000+120

Is there a direct function to convert it to VBScript date?

Thanks.

Ali


[MS] Scott McNairy

unread,
Feb 11, 2004, 3:34:57 PM2/11/04
to
Yes, take this sample script.

set svc = getObject("winmgmts:root\cimv2")
set dt = CreateObject("WbemScripting.SwbemDateTime")
for each obj in svc.ExecQuery("select * from win32_operatingSystem")
wscript.echo obj.localDateTime
dt.Value = obj.localDateTime
wscript.echo dt.GetVarDate(true)
next

--
[MS] Scott McNairy
WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Ali AYEN" <a...@aliayen.com> wrote in message
news:e5BqTSK8...@TK2MSFTNGP09.phx.gbl...

Torgeir Bakken (MVP)

unread,
Feb 11, 2004, 4:24:49 PM2/11/04
to
Ali AYEN wrote:

> I got a datetime value like 20040211140716.500000+120
>
> Is there a direct function to convert it to VBScript date?

Hi

The WMI Date and Time Format is documented here:
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/date_and_time_format.asp

If you are running WinXP or Win2k3 Server, use WbemScripting.SwbemDateTime
(as Scott showed you), for other OSes, you need to parse it yourself.

Below is a function that you can use instead of the
WbemScripting.SWbemDateTime object. Run this script
with cscript.exe in a commend prompt for a demonstration:


Const ISO8601 = -1 ' the international date format everybody should use
sLastAccessed = "20040114183126.489629-480"

Wscript.Echo "ISO 8601: " & ConvWMIDateTime(sLastAccessed, ISO8601)
Wscript.Echo "GeneralDate: " & ConvWMIDateTime(sLastAccessed, vbGeneralDate)
Wscript.Echo "LongDate: " & ConvWMIDateTime(sLastAccessed, vbLongDate)
Wscript.Echo "ShortDate: " & ConvWMIDateTime(sLastAccessed, vbShortDate)
Wscript.Echo "LongTime: " & ConvWMIDateTime(sLastAccessed, vbLongTime)
Wscript.Echo "ShortTime: " & ConvWMIDateTime(sLastAccessed, vbShortTime)


Function ConvWMIDateTime(sDMTFformat, iNamedFormat)

' Author: Torgeir Bakken
' Modified: 2004-01-16
'
' Converts WMI Date and Time Format to standard date/time
'
' WMI Date and Time Format is documented here:
' http://msdn.microsoft.com/library/en-us/wmisdk/wmi/date_and_time_format.asp
'
' Arguments ----->

' DMTFformat: Date string in WMI Date and Time Format
'
' iNamedFormat: Optional. Numeric value that indicates the date/time
' format used. If omitted, ISO 8601 is used.

' -1 (and anything <> 0-4) Display in International Date Format ISO8601
' YYYY-MM-DD hh:nn:ss e.g. 2004-01-15 23:50:44
'
' vbGeneralDate 0 Display a date and/or time. If there is a date part,
' display it as a short date. If there is a time part,
' display it as a long time. If present, both parts are
' displayed.
'
' vbLongDate 1 Display a date using the long date format specified in
' your computer's regional settings.
'
' vbShortDate 2 Display a date using the short date format specified in
' your computer's regional settings.
'
' vbLongTime 3 Display a time using the time format specified in your
' computer's regional settings.
'
' vbShortTime 4 Display a time using the 24-hour format (hh:mm).

Dim sYear, sMonth, sDay, sHour, sMinutes, sSeconds
sYear = mid(sDMTFformat, 1, 4)
sMonth = mid(sDMTFformat, 5, 2)
sDay = mid(sDMTFformat, 7, 2)
sHour = mid(sDMTFformat, 9, 2)
sMinutes = mid(sDMTFformat, 11, 2)
sSeconds = mid(sDMTFformat, 13, 2)

' YYYY-MM-DD hh:nn:ss
ConvWMIDateTime = sYear & "-" & sMonth & "-" & sDay & " " _
& sHour & ":" & sMinutes & ":" & sSeconds

If IsNumeric(iNamedFormat) Then
If iNamedFormat >= 0 And iNamedFormat <= 4 Then
' FormatDateTime will set date format to specified format
ConvWMIDateTime = FormatDateTime(ConvWMIDateTime, iNamedFormat)
End If
End If
End Function


--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


0 new messages