Simple BAT and VBScript to simulate UNIX date /u. Nothing original, I
just stiched together some stuff from the groups. But have not seen
this particular solution before.

========================================================================
== NowUTC.vbs
========================================================================

Option Explicit
Dim UTC
Dim LocalTZ
Dim TZOffset

For Each LocalTZ in
GetObject("winmgmts:").InstancesOf("Win32_ComputerSystem")
TZOffset = LocalTZ.CurrentTimeZone
Next

if TZOffset < 0 Then
UTC = DateAdd("n", ABS(TZOffset), CDate(Now))
else
UTC = DateAdd("n", -ABS(TZOffset), CDate(Now))
end if

Wscript.Echo FmtLZ(Year(UTC), 4) & FmtLZ(Month(UTC), 2) &
FmtLZ(Day(UTC), 2) & FmtLZ(Hour(UTC), 2) & FmtLZ(Minute(UTC), 2) &
FmtLZ(Second(UTC), 2)

Function FmtLZ(NValue, NLength)
FmtLZ = String(NLength - Len(CStr(NValue)), "0") & CStr(NValue)
End Function

=====================================================================
== Test.bat
=====================================================================
@ECHO OFF
:: ------------------------------------------------------------------
:: Run the VBScript and capture its output as the environment
:: variable NowUTC, in the format YYYYMMDDHHNNSS
:: ------------------------------------------------------------------

FOR /F "tokens=*" %%A in ('cscript NowUTC.vbs //nologo') DO SET
NowUTC=%%A

:: ------------------------------------------------------------------
:: As a demo, show NowUTC and how to breakout the parts
:: ------------------------------------------------------------------

ECHO NowUTC env variable is "%NowUTC%"
ECHO The parts of NowUTC are:
ECHO Year = %NowUTC:~0,4%
ECHO Month = %NowUTC:~4,2%
ECHO Day = %NowUTC:~6,2%
ECHO Hour = %NowUTC:~8,2%
ECHO Minute = %NowUTC:~10,2%
ECHO Second = %NowUTC:~12,2%

Re: Current UTC date and time in a batch environment variable by Dr

Dr
Fri Mar 17 14:53:47 CST 2006

JRS: In article <1142539191.932868.300290@z34g2000cwc.googlegroups.com>
, dated Thu, 16 Mar 2006 11:59:51 remote, seen in news:microsoft.public.
scripting.vbscript, alan-spiderman@excite.com posted :
>Simple BAT and VBScript to simulate UNIX date /u. Nothing original, I
>just stiched together some stuff from the groups. But have not seen
>this particular solution before.

Possibly because it's simpler in Javascript.

The following, from <URL:http://www.merlyn.demon.co.uk/batfiles.htm#WSH>
section "Javascript in WSH", will put the UTC date and time on standard
output :-

@echo off
echo WScript.Echo(new Date().toUTCString()); > tmp.js
cscript //nologo tmp.js
del tmp.js

and with a little more code it can be formatted like yours.

>if TZOffset < 0 Then
> UTC = DateAdd("n", ABS(TZOffset), CDate(Now))
>else
> UTC = DateAdd("n", -ABS(TZOffset), CDate(Now))
>end if

What's the point of having two DateAdd lines there?


Your

Wscript.Echo FmtLZ(Year(UTC), 4) & FmtLZ(Month(UTC), 2) &
FmtLZ(Day(UTC), 2) & FmtLZ(Hour(UTC), 2) & FmtLZ(Minute(UTC), 2) &
FmtLZ(Second(UTC), 2)

can be written as

Wscript.Echo ((((Year(UTC)*100 + Month(UTC))*100 + Day(UTC))*100 + _
Hour(UTC))*100 + Minute(UTC))*100 + Second(UTC)

and similar can be done in javascript,

with (new Date()) YMDhms =
((((getUTCFullYear()*100 + getUTCMonth()+1)*100 + getUTCDate())*100 +
getUTCHours())*100 + getUTCMinutes())*100 + getUTCSeconds()

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Re: Current UTC date and time in a batch environment variable by alan-spiderman

alan-spiderman
Thu Mar 23 10:53:03 CST 2006

John,

Thank you very much for the comments. This was my first real VBScript
and I just patched together pieces I found in other scripts. I will
apply your modifications to my running version. Again, thanks for the
help.

- Alan G -


Re: Current UTC date and time in a batch environment variable by Dr

Dr
Thu Mar 23 15:46:42 CST 2006

JRS: In article <1143132783.416018.152710@i39g2000cwa.googlegroups.com>
, dated Thu, 23 Mar 2006 08:53:03 remote, seen in news:microsoft.public.
scripting.vbscript, alan-spiderman@excite.com posted :
>John,
>
>Thank you very much for the comments. This was my first real VBScript
>and I just patched together pieces I found in other scripts. I will
>apply your modifications to my running version. Again, thanks for the
>help.


If you find that, when you start a News reply, Google does not provide
the previous article in quoted form, note what Keith Thompson wrote in
comp.lang.c, message ID <lnwtuhfy7d.fsf@nuthaus.mib.org> :-
If you want to post a followup via groups.google.com, don't use
the "Reply" link at the bottom of the article. Click on "show
options" at the top of the article, then click on the "Reply" at
the bottom of the article headers.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.