however, it only works for Windows XP and Windows Server
2003. It does not work on Windows 2000 Server which is
where I am trying to run the script.
Running the script remotely from a client machine is not
an option because I am doing other things with the script
that need to be run locally. Any ideas? Thanks in
advance.
The string you show is a standard WMI time string, of form
yyyymmddhhnnss.ssssss[+-]tzoffsetminutes
Barring the offset, which can get difficult to calculate due to the lack of
appropriate support in VBScript, you can get the date and time extracted
very easily by using the DateSerial and TimeSerial functions in VBScript.
This isn't perfect - it would be lovely to be able to reproducibly deal with
the time zone offsets - but it should work quite nicely for giving you
comprehensible values.
option explicit
Dim s, serialdate, serialtime
s = "20040114183126.489629"
serialdate = DateSerial(left(s, 4), mid(s, 5, 2), mid(s, 7, 2))
serialtime = TimeSerial( mid(s, 9, 2), mid(s, 11, 2), mid(s, 13, 9) )
wscript.echo serialdate
wscript.echo serialtime
> I am running into a problem because the
> output shows the date in a format like
> 20040114183126.489629-480 instead of something familar
> like 01/15/2004 08:45 AM
> There is a object that I can use:
> 'Set dtmTargetDate = CreateObject
> ("WbemScripting.SWbemDateTime")
>
> however, it only works for Windows XP and Windows Server
> 2003. It does not work on Windows 2000 Server which is
> where I am trying to run the script.
Hi
The WMI Date and Time Format is documented here:
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/date_and_time_format.asp
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
> The string you show is a standard WMI time string, of form
>
> yyyymmddhhnnss.ssssss[+-]tzoffsetminutes
>
> Barring the offset, which can get difficult to calculate due to the lack of
> appropriate support in VBScript, you can get the date and time extracted
> very easily by using the DateSerial and TimeSerial functions in VBScript.
> This isn't perfect - it would be lovely to be able to reproducibly deal with
> the time zone offsets - but it should work quite nicely for giving you
> comprehensible values.
The CIM_DataFile date/time based properties returns my local time in the
yyyymmddhhnnss part, so you don't need to take tzoffsetminutes into
consideration (unless you want to convert the time to GMT).
> s = "20040114183126.489629"
>
> serialdate = DateSerial(left(s, 4), mid(s, 5, 2), mid(s, 7, 2))
> serialtime = TimeSerial( mid(s, 9, 2), mid(s, 11, 2), mid(s, 13, 9) )
> wscript.echo serialdate
> wscript.echo serialtime
G:\TST.VBS(6, 1) Microsoft VBScript runtime error:
Type mismatch: '[string: "26.489629"]'
Line 6 is the TimeSerial line (your script is US centric ;-)
I needed to add SetLocale "en-us" to the script to get it to run without error.
>> .... This isn't perfect - it would be lovely to be able to
>> reproducibly deal with the time zone offsets - but it should work
>> quite nicely for giving you comprehensible values.
>
> The CIM_DataFile date/time based properties returns my local time in the
> yyyymmddhhnnss part, so you don't need to take tzoffsetminutes into
> consideration (unless you want to convert the time to GMT).
Cool. I couldn't remember when I wrote this whether the time was shown
corrected to GMT or as a local value... and I was too tired to try it out.
;)
>> s = "20040114183126.489629"
>>
>> serialdate = DateSerial(left(s, 4), mid(s, 5, 2), mid(s, 7, 2))
>> serialtime = TimeSerial( mid(s, 9, 2), mid(s, 11, 2), mid(s, 13, 9) )
>> wscript.echo serialdate
>> wscript.echo serialtime
>
> G:\TST.VBS(6, 1) Microsoft VBScript runtime error:
> Type mismatch: '[string: "26.489629"]'
>
> Line 6 is the TimeSerial line (your script is US centric ;-)
Figures. Of course, if you think about it, pretty much all of the VBish
date/time functions are - or locale-ish anyway. At least I kept in
character. ;)
That has to be the weakest excuse I have EVER made for anything. :|
> Torgeir Bakken (MVP) wrote:
> >
> > The CIM_DataFile date/time based properties returns my local time in the
> > yyyymmddhhnnss part, so you don't need to take tzoffsetminutes into
> > consideration (unless you want to convert the time to GMT).
>
> Cool. I couldn't remember when I wrote this whether the time was shown
> corrected to GMT or as a local value... and I was too tired to try it out.
> ;)
I see from some previous posts of mine that I mistakenly thought that the time
was shown corrected to GMT (the documentation is very unclear on this point).
> (snip)
> > G:\TST.VBS(6, 1) Microsoft VBScript runtime error:
> > Type mismatch: '[string: "26.489629"]'
> >
> > Line 6 is the TimeSerial line (your script is US centric ;-)
>
> Figures. Of course, if you think about it, pretty much all of the VBish
> date/time functions are - or locale-ish anyway. At least I kept in
> character. ;)
>
> That has to be the weakest excuse I have EVER made for anything. :|
LOL.