在Windows (Windows XP)批处理脚本中,我需要格式化当前日期和时间,以便以后在文件名等中使用。
这类似于堆栈溢出问题如何在批处理文件中添加日期,但也包含时间。
到目前为止,我有这个:
echo %DATE%
echo %TIME%
set datetimef=%date:~-4%_%date:~3,2%_%date:~0,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetimef%
这使:
28/07/2009
8:35:31.01
2009_07_28__ 8_36_01
是否有一种方法可以允许%TIME%中的个位数小时,以便我可以得到以下结果?
2009_07_28__08_36_01
这个脚本使用一个WMI接口,主要通过WMIC工具访问,这是Windows的一个组成部分,因为Windows XP专业版(家庭版也支持,但默认情况下不安装该工具)。该脚本还通过创建和调用WSH vbscript来访问WMI接口,并以与WMIC工具提供的相同格式写入控制台输出时间,从而实现了缺少WMIC工具的解决方案。
@ECHO OFF
REM Returns: RETURN
REM Modify: RETURN, StdOut
REM Required - mandatory: none
REM Required - optionaly: format strings delimited by a space to format an output delimited by predefined delimiter
REM YYYY = 4-digit year
REM MM = 2-digit month
REM DD = 2-digit day
REM hh = 2-digit hour
REM mm = 2-digit minute
REM ss = 2-digit second
REM ms = 3-digit millisecond
CALL :getTime %*
ECHO %RETURN%
GOTO :EOF
REM SUBROUTINE
REM Returns: RETURN
REM Modify: RETURN
REM Required - mandatory: none
REM Required - optionaly: format strings delimited by a space to format an output delimited by predefined delimiter
REM YYYY = 4-digit year
REM MM = 2-digit month
REM DD = 2-digit day
REM hh = 2-digit hour
REM mm = 2-digit minute
REM ss = 2-digit second
REM ms = 3-digit millisecond
:getTime
SETLOCAL EnableDelayedExpansion
SET DELIM=-
WHERE /Q wmic.exe
IF NOT ERRORLEVEL 1 FOR /F "usebackq skip=1 tokens=*" %%x IN (`wmic.exe os get LocalDateTime`) DO (SET DT=%%x & GOTO getTime_Parse)
SET _TMP=%TEMP:"=%
ECHO Wscript.StdOut.WriteLine (GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=@").LocalDateTime)>"%_TMP%\get_time_local-helper.vbs"
FOR /F "usebackq tokens=*" %%x IN (`cscript //B //NoLogo "%_TMP%\get_time_local-helper.vbs"`) DO (SET DT=%%x & GOTO getTime_Parse)
:getTime_Parse
SET _RET=
IF "%1" EQU "" (
SET _RET=%DT:~0,4%%DELIM%%DT:~4,2%%DELIM%%DT:~6,2%%DELIM%%DT:~8,2%%DELIM%%DT:~10,2%%DELIM%%DT:~12,2%%DELIM%%DT:~15,3%
) ELSE (
REM Not recognized format strings are ignored during parsing - no error is reported.
:getTime_ParseLoop
SET _VAL=
IF "%1" EQU "YYYY" SET _VAL=%DT:~0,4%
IF "%1" EQU "MM" SET _VAL=%DT:~4,2%
IF "%1" EQU "DD" SET _VAL=%DT:~6,2%
IF "%1" EQU "hh" SET _VAL=%DT:~8,2%
IF "%1" EQU "mm" SET _VAL=%DT:~10,2%
IF "%1" EQU "ss" SET _VAL=%DT:~12,2%
IF "%1" EQU "ms" SET _VAL=%DT:~15,3%
IF DEFINED _VAL (
IF DEFINED _RET (
SET _RET=!_RET!%DELIM%!_VAL!
) ELSE (
SET _RET=!_VAL!
)
)
SHIFT
IF "%1" NEQ "" GOTO getTime_ParseLoop
)
ENDLOCAL & SET RETURN=%_RET%
GOTO :EOF
这个批处理脚本将完全按照O.P.的要求执行(在Windows XP SP3上测试)。
我还使用了前面“jph”所描述的聪明的注册表技巧,我认为这是在任何新的或旧的Windows系统上获得100%一致的日期格式“yyyy_MM_dd”的最简单方法。对一个注册表值的更改是瞬时、临时和微不足道的;它只持续几毫秒,然后立即恢复。
双击此批处理文件进行即时演示,命令提示符窗口将弹出并显示您的时间戳. . . . .
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: generates a custom formatted timestamp string using date and time.
:: run this batch file for an instant demo.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ECHO OFF
SETLOCAL & MODE CON:COLS=80 LINES=15 & COLOR 0A
:: --- CHANGE THE COMPUTER DATE FORMAT TEMPORARILY TO MY PREFERENCE "yyyy_MM_dd",
REG COPY "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f 2>nul >nul
REG ADD "HKCU\Control Panel\International" /v sShortDate /d "yyyy_MM_dd" /f 2>nul >nul
SET MYDATE=%date%
:: --- REVERT COMPUTER DATE BACK TO SYSTEM PREFERENCE
REG COPY "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f 2>nul >nul
REG DELETE "HKCU\Control Panel\International-Temp" /f 2>nul >nul
:: --- SPLIT THE TIME [HH:MM:SS.SS] TO THREE SEPARATE VARIABLES [HH] [MM] [SS.SS]
FOR /F "tokens=1-3 delims=:" %%A IN ('echo %time%') DO (
SET HOUR=%%A
SET MINUTES=%%B
SET SECONDS=%%C
)
:: --- CHOOSE ONE OF THESE TWO OPTIONS :
:: --- FOR 4 DIGIT SECONDS //REMOVES THE DOT FROM THE SECONDS VARIABLE [SS.SS]
:: SET SECONDS=%SECONDS:.=%
:: --- FOR 2 DIGIT SECONDS //GETS THE FIRST TWO DIGITS FROM THE SECONDS VARIABLE [SS.SS]
SET SECONDS=%SECONDS:~0,2%
:: --- FROM 12 AM TO 9 AM, THE HOUR VARIABLE WE EXTRACTED FROM %TIME% RETURNS A SINGLE DIGIT,
:: --- WE PREFIX A ZERO CHARACTER TO THOSE CASES, SO THAT OUR WANTED TIMESTAMP
:: --- ALWAYS GENERATES DOUBLE-DIGIT HOURS (24-HOUR CLOCK TIME SYSTEM).
IF %HOUR%==0 (SET HOUR=00)
IF %HOUR%==1 (SET HOUR=01)
IF %HOUR%==2 (SET HOUR=02)
IF %HOUR%==3 (SET HOUR=03)
IF %HOUR%==4 (SET HOUR=04)
IF %HOUR%==5 (SET HOUR=05)
IF %HOUR%==6 (SET HOUR=06)
IF %HOUR%==7 (SET HOUR=07)
IF %HOUR%==8 (SET HOUR=08)
IF %HOUR%==9 (SET HOUR=09)
:: --- GENERATE OUR WANTED TIMESTAMP
SET TIMESTAMP=%MYDATE%__%HOUR%_%MINUTES%_%SECONDS%
:: --- VIEW THE RESULT IN THE CONSOLE SCREEN
ECHO.
ECHO Generate a custom formatted timestamp string using date and time.
ECHO.
ECHO Your timestamp is: %TIMESTAMP%
ECHO.
ECHO.
ECHO Job is done. Press any key to exit . . .
PAUSE > NUL
EXIT
这是我对adatetime字符串的2美分。在MM DD YYYY系统上切换第一个和第二个%DATE:~条目。
REM ====================================================================================
REM CREATE UNIQUE DATETIME STRING FOR ADDING TO FILENAME
REM ====================================================================================
REM Can handle dd DDxMMxYYYY and DDxMMxYYYY > CREATES YYYYMMDDHHMMSS (x= any character)
REM ====================================================================================
REM CHECK for SHORTDATE dd DDxMMxYYYY
IF "%DATE:~0,1%" GTR "3" (
SET DATETIME=%DATE:~9,4%%DATE:~6,2%%DATE:~3,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
) ELSE (
REM ASSUMES SHORTDATE DDxMMxYYYY
SET DATETIME=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
)
REM CORRECT FOR HOURS BELOW 10
IF %DATETIME:~8,2% LSS 10 SET DATETIME=%DATETIME:~0,8%0%DATETIME:~9,5%
ECHO %DATETIME%