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

UTC date in VBScript

2,158 views
Skip to first unread message

jer...@my-deja.com

unread,
Oct 31, 2000, 1:46:46 PM10/31/00
to
Hi

Is there a function in VBScript to get the current UTC or GMT date?

TIA


Sent via Deja.com http://www.deja.com/
Before you buy.

Michael Harris

unread,
Oct 31, 2000, 10:43:13 PM10/31/00
to

VBScript doesn't have anything built in... What host is running the script?

Here's a VBScript function that emulates the JScript Date().toUTCString() method but it reads the
registry for ActiveTimeBias needed in the calculation.

msgbox FormatUTC(now)

function FormatUTC(ByVal vDate)
'================================================
'Converts input date/time to Universal Time
'Coordinate (UTC) in the same format as JScript's
'Date().toUTCString() method.
'
' dayabbreviation, dd monthname yy hh:mm:ss UTC
'
'Example: Fri, 2 Jul 1999 18:17:50 UTC
'
'================================================
if isdate(vdate) then
vdate = cdate(vdate)
else
err.raise &h7001,"FormatUTC","Argument is not a valid date"
end if

set shell = createobject("wscript.shell")

'ActiveTimeBias is the number of minutes that UTC
'is offset from local time. It is added to local
'date/time to get UTC equivalent.
'
strValueName = _
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\"_
& "TimeZoneInformation\ActiveTimeBias"
tmpTimeOffset = shell.regread(strValueName)

'ActiveTimeBias can be REG_BINARY or REG_DWORD
'depending on the OS version...
'
if IsArray(tmpTimeOffset) then
'it's REG_BINARY w/ 2 bytes significant
lngTimeOffset = tmpTimeOffset(0) + (tmpTimeOffset(1)*(2^8))
else
'it's REG_DWORD
lngTimeOffset = tmpTimeOffset
end if
dt = DateAdd("n", lngTimeOffset, vDate)

'Extract the "fields" of the date/time and
'reformat into UTC standard format.
'
dayabbr = weekdayname(weekday(dt),True) 'abbreviated

'JScript doesn't include leading 0 on day,
'so we don't either.
'
dd = datepart("d",dt)
monnm = monthname(month(dt),true)
yyyy = year(dt)

hh = right("00" & hour(dt),2)
mn = right("00" & minute(dt),2)
ss = right("00" & second(dt),2)

FormatUTC = _
dayabbr & ", " _
& dd & " " & monnm & " " & yyyy & " " _
& hh & ":" & mn & ":" & ss _
& " UTC"

end function

--
Michael Harris
Microsoft.MVP.Scripting
--
<jer...@my-deja.com> wrote in message news:8tn42i$62a$1...@nnrp1.deja.com...

0 new messages