有没有办法使用 Windows 命令获得纪元时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3454112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to get epoch time using a Windows command?
提问by moorecats
Is there a way to get epoch time using a Windows command? If not, can the date
and time
commands output be modified?
有没有办法使用 Windows 命令获得纪元时间?如果没有,可以修改date
和time
命令输出吗?
For example date
in Windows gives the date with / etc. I would like to get an output that has no special characters such as / :
例如date
在 Windows 中用 / 等给出日期。我想得到一个没有特殊字符的输出,例如 / :
采纳答案by Leniel Maccaferri
To get time in the "yyyymmdd" format, try this:
要以“yyyymmdd”格式获取时间,请尝试以下操作:
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
http://www.sprint.net.au/~terbut/usefulbox/msdoscmds.htm
Also you can do it this way:
你也可以这样做:
echo %date:~10,4%%date:~4,2%%date:~7,2%
回答by Benjamin2002
Use this command to show numbers of seconds after epoch.
使用此命令显示纪元后的秒数。
(Cmd command)
(Cmd 命令)
powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date)).TotalSeconds"
powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date)).TotalSeconds"
回答by Cameron Oakenleaf
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:
在 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.
这将 %epoch% 变量设置为当前的 unix 时间戳,我可以根据需要使用它。
Hope this helps.
希望这可以帮助。
回答by Preet Sangha
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]
[根据 Kurt Pfeifle 关于时间扩展空间的评论进行编辑]
回答by Joey
There is no reliable way of getting a date in batch files without resorting to external tools or other languages such as VBScript.
在不求助于外部工具或其他语言(如 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.
在 VBScript 中,您可以使用Date
和Time
函数访问当前日期和时间。FormatDateTime
将为您提供文化中立的日期/时间格式,然后您可以对其进行解析。
You can get a result frmo the VBScript by using WScript.Echo
from within the script and calling it like so from the batch:
您可以通过WScript.Echo
在脚本中使用并从批处理中像这样调用它来从VBScript 中获得结果:
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.
然后该变量%result%
包含 VBScript 在其第一行中作为输出的任何内容。
回答by Arjen Rodenhuis
The CoreUtils for Windows project, http://gnuwin32.sourceforge.net/packages/coreutils.htmhas a date
command, which give you the same options as under Linux.
CoreUtils for Windows 项目http://gnuwin32.sourceforge.net/packages/coreutils.htm有一个date
命令,它为您提供与 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.
下载软件,重命名date.exe
为gnudate.exe
,避免与Dos date
命令冲突。您需要这些文件libintl-2.dll
并libiconv-2.dll
运行命令。
For all available options type:
对于所有可用的选项类型:
gnudate --help
For example gnudate "+%a %e %b %Y %H:%M:%S"
will give:
例如gnudate "+%a %e %b %Y %H:%M:%S"
会给:
Sun 10 apr 2016 21:52:35
The command gnudate +%s
will give the seconds since Epoch:
该命令gnudate +%s
将给出自 Epoch 以来的秒数:
1460325461
The next Dos batch file shows the usage of gnudate. You will need to double the %
in the gnudate +%s
parameter.
下一个 Dos 批处理文件显示了 gnudate 的用法。您需要%
将 gnudate+%s
参数中的 加倍。
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.
该批处理文件在 Linux 下的虚拟机中使用 Windows 8 进行了测试。
To run it, you'll need to install ffmpeg
.
You can download the Static build
from https://ffmpeg.zeranoe.com/builds/.
要运行它,您需要安装ffmpeg
.
您可以Static build
从https://ffmpeg.zeranoe.com/builds/下载。