Cmd 有没有办法在Windows命令提示符下以秒为单位获取当前时间?

Cmd 有没有办法在Windows命令提示符下以秒为单位获取当前时间?,cmd,Cmd,我的目标是创建并命名一个以当前时间(以秒为单位)为名称的文件。在IOS中有我使用的touch“$(date+%s)\u my\u migration\u name.up.sql”,但是我正在为Windows寻找类似的解决方案。可以使用子例程GetSeconds获取自1970-01-01 00:00:00分配给环境变量(如seconds)的秒数,如下批处理文件代码所示: @echo off call :GetSeconds set "FileName=%Seconds%_my_migra

我的目标是创建并命名一个以当前时间(以秒为单位)为名称的文件。在IOS中有我使用的
touch“$(date+%s)\u my\u migration\u name.up.sql”
,但是我正在为Windows寻找类似的解决方案。

可以使用子例程
GetSeconds
获取自1970-01-01 00:00:00分配给环境变量(如
seconds
)的秒数,如下批处理文件代码所示:

@echo off
call :GetSeconds
set "FileName=%Seconds%_my_migration_name.up.sql"
set FileName
pause
goto :EOF

:GetSeconds
setlocal EnableExtensions DisableDelayedExpansion
rem Get current date/time region (country) independent.
if exist %SystemRoot%\System32\robocopy.exe for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "Year=%%I" & set "Month=%%J" & set "Day=%%K" & set "Hour=%%L" & set "Minute=%%M" & set "Second=%%N" & goto ProcessDateTime
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "DateTime=%%I"
set "Year=%DateTime:~0,4%" & set "Month=%DateTime:~4,2%" & set "Day=%DateTime:~6,2%" & set "Hour=%DateTime:~8,2%" & set "Minute=%DateTime:~10,2%" & set "Second=%DateTime:~12,2%"

:ProcessDateTime
rem echo Current date/time is: %Year%-%Month%-%Day% %Hour%:%Minute%:%Second%
rem Remove leading zeros from the date/time values or calculation could be wrong.
if %Month:~0,1%  == 0 set "Month=%Month:~1%"
if %Day:~0,1%    == 0 set "Day=%Day:~1%"
if %Hour:~0,1%   == 0 set "Hour=%Hour:~1%"
if %Minute:~0,1% == 0 set "Minute=%Minute:~1%"
if %Second:~0,1% == 0 set "Second=%Second:~1%"

set /A "Index1=Year-1979"
set /A "Index2=Index1-30"
if %Index1% LEQ 30 (
    rem Get number of days to year for the years 1980 to 2009.
    for /F "tokens=%Index1% delims= " %%Y in ("3652 4018 4383 4748 5113 5479 5844 6209 6574 6940 7305 7670 8035 8401 8766 9131 9496 9862 10227 10592 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245") do set "Days=%%Y"
    for /F "tokens=%Index1% delims= " %%L in ("Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N") do set "LeapYear=%%L"
) else (
    rem Get number of days to year for the years 2010 to 2038.
    for /F "tokens=%Index2% delims= " %%Y in ("14610 14975 15340 15706 16071 16436 16801 17167 17532 17897 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550 21915 22280 22645 23011 23376 23741 24106 24472 24837") do set "Days=%%Y"
    for /F "tokens=%Index2% delims= " %%L in ("N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N") do set "LeapYear=%%L"
)

rem Add the days to month in year.
if "%LeapYear%" == "N" (
    for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
) else (
    for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
)

rem Add the complete days in month of year.
set /A "Days+=Day-1"

rem Calculate the seconds which is easy now.
set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"
endlocal & set "Seconds=%Seconds%"
rem echo Seconds since 1970:   %Seconds%

rem Exit this subroutine.
goto :EOF
有关解释,请参见以下答案:

子例程
GetSeconds
在环境变量
Seconds
中返回日期/时间范围1980-01-01 00:00:00到2038-01-19 03:14:07自1970-01-01 00:00以来的秒数。因此,代码必须在2038年更改

注释(备注)可以全部从代码中删除,以便更快地处理批处理文件代码。

我认为这与您的问题有关。