15

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?

Eitan T
  • 32,660
  • 14
  • 72
  • 109
beary605
  • 802
  • 2
  • 9
  • 18

6 Answers6

24

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!

Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • 3
    This is a good answer, but I seem to have found a bug in the code. For the first two months of the year, the results are two years into the future. Where you have `4800+z`, I think you need `4800-z`. Refer to Richie Lawrence's original DateToSecs code. If you have time, please verify my fix (and edit your post). – jimhark Jan 17 '13 at 21:36
  • 3
    Good. Thanks. I've made another change in my copy. It's not really a bug, but I thought I'd share it with you. I took this code apart to try to fully understand it. I figured out you don't need to add 4800 if you change `-2472633` to `-719469`. It just backs out an adjustment that's not needed here. So you wind up with the two lines ending with `10000%Year%%%10000-z` and `Day-719469`. Not a big difference, but now it's easier to explain what 719469 is, it's the number of days in 1970 years, used to adjust to the epoch. – jimhark Jan 19 '13 at 12:54
  • @jimhark Once again, you're right. Not a big difference indeed, but I always favor the simpler solution, so I fixed my answer according to your suggestion. – Eitan T Jan 20 '13 at 08:11
  • 2
    Oh thanks, that's great. I've exchanged email with Richie Lawrence. I was amazed how quickly he replied to my first email asking him about the 4800 adjustment. I wrote him back with a fairly complete justification for removing it. The correction is (apparently) there to support Julian Day which has an epoch of January 1, 4713 BC (proleptic Julian calendar). But our final calculation is seconds from 01/01/1970 and Windows Batch uses 32-bit signed integers which limits the earliest date to 1901/12/13. – jimhark Jan 20 '13 at 11:36
7

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
mguven guven
  • 91
  • 1
  • 5
3

What about simple 1-line long C program returning UNIX timestamp? You can retrieve value from %errorlevel% in batch script.

#include <stdio.h>
#include <time.h>

int main(void)
{
    return (int) time(NULL);
}

In my test in command prompt it worked:

C:\Users\dabaran\Desktop\cs50\src\C>.\time || echo %errorlevel% && set mytstamp=%errorlevel%
1419609373

C:\Users\dabaran\Desktop\cs50\src\C>echo %mytstamp%
1419609373
damian1baran
  • 1,307
  • 1
  • 14
  • 13
2

There's a really simple way of doing this in a single batch file with no external scripts, files, libraries, etc.

<!-- :
  for /f "tokens=* usebackq" %%a in (`start /b cscript //nologo "%~f0?.wsf"`) do (set timestamp=%%a)
  echo %timestamp%
  pause
  exit /b
-->

<job><script language="JavaScript">
  WScript.Echo(new Date().getTime());
</script></job>

The way it works is, code for a batch script AND code for a JS file are contained in the same .cmd or .bat file. you can force cscript to run the batch file as a script by commenting out the batch code and the : after the first line means batch will ignore it and run the batch code directly. so there you go!

spacefluff432
  • 566
  • 3
  • 13
0

There is no batch command for returning UNIX time. Your only options would be to write a program which could be run from a batch file that would return the UNIX time, or you could use the Windows PowerShell.

ctor
  • 5,938
  • 2
  • 26
  • 37
  • 1
    Surprisingly enough, there _is_ a batch way... see my answer. – Eitan T Jul 08 '12 at 19:18
  • 2
    for powershell equivalent command would be `get-date -uformat %s` (or `powershell -command get-date -uformat %s` to call it from cmd/batch – wmz Jul 08 '12 at 19:28
0

By far best solution is to download a freestanding date.exe unix-port.

Recommend that you rename it to unixdate.exe, to avoid conflict with MS Date command.

Get it from here

  1. download & unzip. As far as I know, all the utilities are 'portable', that is, they don't require any installation program for them to work. I've only used a few (rm.exe & date.exe). Also, you can ReName the exe's in case they might match other system utils you already have
  2. Find the actual executable ports in the subfolder (usr\local\wbin)
  3. To get HELP info type the command followed with --help (must spell help in lowercase)

Example:

((PROMPT)):unixdate +%Y%M%d
20141704

((PROMPT)):unixdate +%Y%b%d
2014Sep04
Tasos K.
  • 7,979
  • 7
  • 39
  • 63