是否有一种方法可以在Windows命令提示符中以秒为单位获得当前时间?



我的目标是创建并命名一个以当前时间(以秒为单位)作为名称的文件。在IOS中有我使用的touch "$(date +%s)_my_migration_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%System32robocopy.exe for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%System32robocopy.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%System32wbemwmic.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

的解释见以下答案:

  • 批处理文件删除超过N天的文件
  • 午夜后时间设置不正确
  • GOTO:EOF返回到哪里?
  • 使用Windows批处理文件的单行多命令

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

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

从https://ss64.com/nt/time.html,我看到您可以只是回显当前时间,包括秒(以及额外的百分之一秒)

c:>echo %TIME%
18:13:52.15

最新更新