Is there a way to get epoch time using a Windows command?

Use this command to show numbers of seconds after epoch.

(Cmd command)

powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date)).TotalSeconds"


Through my own research online, I was not able to find a way to do this via a batch file directly. However, I was able to find this solution that worked for me:

In toEpoch.vbs:

WScript.Echo DateDiff("s", "01/01/1970 00:00:00", Now())

Then called from my batch script like so:

for /f "delims=" %%x in ('cscript /nologo toEpoch.vbs') do set epoch=%%x

That set the %epoch% variable with the current unix timestamp and I was able to use it as I needed to.

Hope this helps.


from the command line try this

for /f "tokens=2,3,4 delims=/ " %f in ('date /t') do @echo %h%g%f

remember to double up the % chars if in batch file

@echo off
setlocal
for /f "tokens=2,3,4 delims=/ " %%f in ('date /t') do set d=%%h%%g%%f
for /f "tokens=1,2 delims=: " %%f in ('time /t') do set t=%%f%%g
echo datetime is : %d%%t: =0%
endlocal

I got this output:

c:\development>xx.bat
datetime is : 201008111108

[Edited per Kurt Pfeifle's comment about spaces in time expansion]