问题描述
据我所知,批不具有使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.
是否有一个命令,或设置批处理命令,使用它可以得到UNIX时间的?
Is there a command, or set of commands in Batch with which you can get the UNIX time?
推荐答案
有的 (使用GetDate和GetTime).
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).
下面是一个简单的脚本,它采用一个小WMI:
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
结果将返回到传递到 GetUnixTime
第一个参数,即%UNIX_TIME%
。
例如:
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
希望它帮助!
这篇关于批次:时间戳UNIX时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!