Is there a way to get epoch time using a Windows c

2019-01-19 17:08发布

问题:

Is there a way to get epoch time using a Windows command? If not, can the date and time commands output be modified?

For example date in Windows gives the date with / etc. I would like to get an output that has no special characters such as / :

回答1:

To get time in the "yyyymmdd" format, try this:

for /F "tokens=2-4 delims=/ " %i in ('date /t') do echo %k%i%j

More on:

http://www.sprint.net.au/~terbut/usefulbox/msdoscmds.htm



回答2:

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.



回答3:

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"



回答4:

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]



回答5:

There is no reliable way of getting a date in batch files without resorting to external tools or other languages such as VBScript.

From VBScript you can access the current date and time with the Date and Time functions. FormatDateTime will get you a culture-neutral date/time format which you can then parse.

You can get a result frmo the VBScript by using WScript.Echo from within the script and calling it like so from the batch:

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

Then the variable %result% contains whatever the VBScript had as output in its first line.



回答6:

The CoreUtils for Windows project, http://gnuwin32.sourceforge.net/packages/coreutils.htm has a date command, which give you the same options as under Linux.

Download the software, and rename date.exe to gnudate.exe, to avoid a conflict with the Dos date command. You need the files libintl-2.dll and libiconv-2.dll to run the command.

For all available options type:

gnudate --help 

For example gnudate "+%a %e %b %Y %H:%M:%S" will give:

Sun 10 apr 2016 21:52:35

The command gnudate +%s will give the seconds since Epoch:

1460325461

The next Dos batch file shows the usage of gnudate. You will need to double the % in the gnudate +%s parameter.

rem set the variable s to the epoch seconds.
for /f "tokens=1 delims=" %%A in ('gnudate +%%s') do set s=%%A
rem use `%s%` for the time offset parameter of the ffmpeg drawtext filter.
ffmpeg -y -f lavfi -i testsrc=duration=15.3:size=cif:r=10 -vf "drawtext=fontfile=arial.ttf:text=%%{pts\\\:localtime\\\:%s%\\\:%%a %%d %%b %%Y %%H\\\\\\:%%M\\\\\\:%%S}:fontsize=10:x=w-text_w:y=h-lh:box=1" a.mp4

ffplay a.mp4

This batch file was tested with Windows 8 in a virtual machine under Linux.

To run it, you'll need to install ffmpeg.
You can download the Static build from https://ffmpeg.zeranoe.com/builds/.