Discussion:
UTC Time
(too old to reply)
SR
2007-05-07 10:35:00 UTC
Permalink
Hi Group

How will i get the time in UTC format through VB script.
Thanks in Advance
Tom Lavedas
2007-05-07 17:55:09 UTC
Permalink
Post by SR
Hi Group
How will i get the time in UTC format through VB script.
Thanks in Advance
Here is an oldie, based on a posting by Walter Zackery from circa
2000 ...

wsh.echo ToUTC(now)

Function ToUTC(vDate)
with CreateObject("htmlfile")
.write("<script>var a = new Date('" & vDate & "');")
.write("a.setFullYear(" & Year(vDate) & ");")
.write("a = a.toUTCString();</script>")
.close
ToUTC = .parentWindow.a
end with
End Function

It accesses the JScript script engine inherent in WSH to use a
function available there that is not available to VBS.

Tom Lavedas
==========
http://members.cox.net/tglbatch/wsh/
Dr J R Stockton
2007-05-08 16:38:28 UTC
Permalink
In microsoft.public.scripting.vbscript message <1178560509.915804.156400
@l77g2000hsb.googlegroups.com>, Mon, 7 May 2007 10:55:09, Tom Lavedas
Post by Tom Lavedas
Post by SR
Hi Group
How will i get the time in UTC format through VB script.
Thanks in Advance
Here is an oldie, based on a posting by Walter Zackery from circa
2000 ...
wsh.echo ToUTC(now)
Function ToUTC(vDate)
with CreateObject("htmlfile")
.write("<script>var a = new Date('" & vDate & "');")
.write("a.setFullYear(" & Year(vDate) & ");")
.write("a = a.toUTCString();</script>")
.close
ToUTC = .parentWindow.a
end with
End Function
It accesses the JScript script engine inherent in WSH to use a
function available there that is not available to VBS.
For the current time/date, I see no point in passing in a VBS 'Now'
(resolution : one second), converted to a string, for Javascript to
parse. Indeed, with all combinations of preferences, can one be sure
that the VBS string will necessarily be parsed correctly? With

.write("<script>var a = new Date();")

Javascript will get the date/time. setFullYear will not be needed.
Granted, toUTCString() will not return fractional seconds but if wanted
they could be added.

Actually, if the OP wants a CDate representing the current UTC, get
Javascript to return new Date()/864e5 + N and get VBS to CDate()
that. N is the daycount from 1899-12-30 to 1970-01-01, 25569, easily
found empirically. (+new Date() is ms from 1970.0 UTC.)

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Richard Mueller [MVP]
2007-05-08 18:14:59 UTC
Permalink
Post by SR
Hi Group
How will i get the time in UTC format through VB script.
Thanks in Advance
And here is a VBScript solution:
====
Option Explicit

Dim dtmDateValue, dtmAdjusted
Dim objShell, lngBiasKey, lngBias, k

dtmDateValue = Now()

' Obtain local Time Zone bias from machine registry.
Set objShell = CreateObject("Wscript.Shell")
lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _
& "TimeZoneInformation\ActiveTimeBias")
If (UCase(TypeName(lngBiasKey)) = "LONG") Then
lngBias = lngBiasKey
ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then
lngBias = 0
For k = 0 To UBound(lngBiasKey)
lngBias = lngBias + (lngBiasKey(k) * 256^k)
Next
End If

' Convert datetime value to UTC.
dtmAdjusted = DateAdd("n", lngBias, dtmDateValue)

Wscript.Echo dtmAdjusted
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
SR
2007-05-11 04:44:00 UTC
Permalink
Thanks, it worked for me ..
Post by Richard Mueller [MVP]
Post by SR
Hi Group
How will i get the time in UTC format through VB script.
Thanks in Advance
====
Option Explicit
Dim dtmDateValue, dtmAdjusted
Dim objShell, lngBiasKey, lngBias, k
dtmDateValue = Now()
' Obtain local Time Zone bias from machine registry.
Set objShell = CreateObject("Wscript.Shell")
lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _
& "TimeZoneInformation\ActiveTimeBias")
If (UCase(TypeName(lngBiasKey)) = "LONG") Then
lngBias = lngBiasKey
ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then
lngBias = 0
For k = 0 To UBound(lngBiasKey)
lngBias = lngBias + (lngBiasKey(k) * 256^k)
Next
End If
' Convert datetime value to UTC.
dtmAdjusted = DateAdd("n", lngBias, dtmDateValue)
Wscript.Echo dtmAdjusted
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
Continue reading on narkive:
Loading...