Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UTC time with windows batch file

I have been trying to find a way of getting a windows batch file to display the current UTC time when run. So in other words get the current amount of milliseconds since it was initiated in 1970.

Does anyone know how to do this.

like image 774
AlanF Avatar asked Dec 16 '22 02:12

AlanF


2 Answers

Using WMI:

for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do set %%x

This will set the variables Day, DayofWeek, Hour, Minute, Month, Quarter, Second, WeekInMonth and Year which you can use, then.

You won't get a time with Unix epoch from Windows easily, though. If you have PowerShell you can use

[long]((date).touniversaltime()-[datetime]'1970-01-01').totalmilliseconds

which you can call from a batchfile via

powershell "..."

But in that case you could write your batch file in a real language anyway.

like image 142
Joey Avatar answered Mar 29 '23 17:03

Joey


Alternative:

REM get UTC times:
for /f %%a in ('wmic Path Win32_UTCTime get Year^,Month^,Day^,Hour^,Minute^,Second /Format:List ^| findstr "="') do (set %%a)
Set Second=0%Second%
Set Second=%Second:~-2%
Set Minute=0%Minute%
Set Minute=%Minute:~-2%
Set Hour=0%Hour%
Set Hour=%Hour:~-2%
Set Day=0%Day%
Set Day=%Day:~-2%
Set Month=0%Month%
Set Month=%Month:~-2%
set UTCTIME=%Hour%:%Minute%:%Second%
set UTCDATE=%Year%%Month%%Day%
like image 27
BJK Avatar answered Mar 29 '23 17:03

BJK