Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch: Timestamp to UNIX Time

For all I know, Batch does not have a command that gives the UNIX time. The closest one I can find is %time%, which only displays the timestamp.

Is there a command, or set of commands in Batch with which you can get the UNIX time?

like image 567
beary605 Avatar asked Jul 08 '12 17:07

beary605


Video Answer


2 Answers

create a .bat file called "getUtime.bat"

@echo off
echo WScript.Echo(new Date().getTime()); > %temp%\time.js
cscript //nologo %temp%\time.js
del %temp%\time.js

and call like this

"C:\>getUTime"
1430894237616
like image 120
mguven guven Avatar answered Sep 19 '22 15:09

mguven guven


There's Richie Lawrence's batch library that has all those nifty handy scripts. The one you need is DateToSec (which uses GetDate and GetTime).

Here's a simplified script, that employs a little WMI:

@echo off
setlocal
call :GetUnixTime UNIX_TIME
echo %UNIX_TIME% seconds have elapsed since 1970-01-01 00:00:00
goto :EOF

:GetUnixTime
setlocal enableextensions
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do (
    set %%x)
set /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z
set /a ut=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469
set /a ut=ut*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100
endlocal & set "%1=%ut%" & goto :EOF

The result will be returned into the first parameter passed to GetUnixTime, i.e. %UNIX_TIME%.
For example:

1341791426 seconds have elapsed since 1970-01-01 00:00:00

Hope it helps!

like image 20
Eitan T Avatar answered Sep 20 '22 15:09

Eitan T