在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
REM Assumes UK style date format for date environment variable (DD/MM/YYYY).
REM Assumes times before 10:00:00 (10am) displayed padded with a space instead of a zero.
REM If first character of time is a space (less than 1) then set DATETIME to:
REM YYYY-MM-DD-0h-mm-ss
REM Otherwise, set DATETIME to:
REM YYYY-MM-DD-HH-mm-ss
REM Year, month, day format provides better filename sorting (otherwise, days grouped
REM together when sorted alphabetically).
IF "%time:~0,1%" LSS "1" (
SET DATETIME=%date:~6,4%-%date:~3,2%-%date:~0,2%-0%time:~1,1%-%time:~3,2%-%time:~6,2%
) ELSE (
SET DATETIME=%date:~6,4%-%date:~3,2%-%date:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
)
ECHO %DATETIME%
也许是这样的:
@call:DateTime
@for %%? in (
"Year :Y"
"Month :M"
"Day :D"
"Hour :H"
"Minutes:I"
"Seconds:S"
) do @for /f "tokens=1-2 delims=:" %%# in (%%?) do @for /f "delims=" %%_ in ('echo %%_DT_%%$_%%') do @echo %%# : _DT_%%$_ : %%_
:: OUTPUT
:: Year : _DT_Y_ : 2014
:: Month : _DT_M_ : 12
:: Day : _DT_D_ : 17
:: Hour : _DT_H_ : 09
:: Minutes : _DT_I_ : 04
:: Seconds : _DT_S_ : 35
@pause>nul
@goto:eof
:DateTime
@verify errorlevel 2>nul & @wmics Alias /? >nul 2>&1
@if not errorlevel 1 (
@for /f "skip=1 tokens=1-6" %%a in ('wmic path win32_localtime get day^,hour^,minute^,month^,second^,year /format:table') do @if not "%%f"=="" ( set "_DT_D_=%%a" & set "_DT_H_=%%b" & set "_DT_I_=%%c" & set "_DT_M_=%%d" & set "_DT_S_=%%e" & set "_DT_Y_=%%f" )
) else (
@set "_DT_T_=1234567890 "
)
@if errorlevel 1 (
@for %%? in ("iDate" "sDate" "iTime" "sTime" "F" "Y" "M" "D" "H" "I" "S") do @set "_DT_%%~?_=%%~?"
@for %%? in ("Date" "Time") do @for /f "skip=2 tokens=1,3" %%a in ('reg query "HKCU\Control Panel\International" /v ?%%~? 2^>nul') do @for /f %%x in ('echo:%%_DT_%%a_%%') do @if "%%x"=="%%a" set "_DT_%%a_=%%b"
@for /f "tokens=1-3 delims=%_DT_T_%" %%a in ("%time%") do @set "_DT_T_=%%a%%b%%c"
)
@if errorlevel 1 (
@if "%_DT_iDate_%"=="0" (set "_DT_F_=_DT_D_ _DT_Y_ _DT_M_") else if "%_DT_iDate_%"=="1" (set "_DT_F_=_DT_D_ _DT_M_ _DT_Y_") else if "%_DT_iDate_%"=="2" (set "_DT_F_=_DT_Y_ _DT_M_ _DT_D_")
@for /f "tokens=1-4* delims=%_DT_sDate_%" %%a in ('date/t') do @for /f "tokens=1-3" %%x in ('echo:%%_DT_F_%%') do @set "%%x=%%a" & set "%%y=%%b" & set "%%z=%%c"
@for /f "tokens=1-3 delims=%_DT_T_%" %%a in ("%time%") do @set "_DT_H_=%%a" & set "_DT_I_=%%b" & set "_DT_S_=%%c"
@for %%? in ("iDate" "sDate" "iTime" "sTime" "F" "T") do @set "_DT_%%~?_="
)
@for %%i in ("Y" ) do @for /f %%j in ('echo:"%%_DT_%%~i_%%"') do @set /a _DT_%%~i_+= 0 & @for /f %%k in ('echo:"%%_DT_%%~i_:~-4%%"') do @set "_DT_%%~i_=%%~k"
@for %%i in ("M" "D" "H" "I" "S") do @for /f %%j in ('echo:"%%_DT_%%~i_%%"') do @set /a _DT_%%~i_+=100 & @for /f %%k in ('echo:"%%_DT_%%~i_:~-2%%"') do @set "_DT_%%~i_=%%~k"
@exit/b
在这种情况下,使用简单、标准的编程方法:
与其花费大量精力解析未知实体,不如简单地保存当前配置,将其重置为已知状态,提取信息,然后恢复原始状态。只使用标准的Windows资源。
具体来说,日期和时间格式存储在注册表项下
HKCU\Control Panel\International\ [MS定义]"values": " timeformat "及"sShortDate"。
Reg是包含在所有Windows版本中的控制台注册表编辑器。
修改HKCU密钥不需要提升权限
Prompt $N:$D $T$G
::Save current config to a temporary (unique name) subkey, Exit if copy fails
Set DateTime=
Set ran=%Random%
Reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp%ran%" /f
If ErrorLevel 1 GoTO :EOF
::Reset the date format to your desired output format (take effect immediately)
::Resetting the time format is useless as it only affect subsequent console windows
::Reg add "HKCU\Control Panel\International" /v sTimeFormat /d "HH_mm_ss" /f
Reg add "HKCU\Control Panel\International" /v sShortDate /d "yyyy_MM_dd" /f
::Concatenate the time and (reformatted) date strings, replace any embedded blanks with zeros
Set DateTime=%date%__%time:~0,2%_%time:~3,2%_%time:~6,2%
Set DateTime=%DateTime: =0%
::Restore the original config and delete the temp subkey, Exit if restore fails
Reg copy "HKCU\Control Panel\International-Temp%ran%" "HKCU\Control Panel\International" /f
If ErrorLevel 1 GoTO :EOF
Reg delete "HKCU\Control Panel\International-Temp%ran%" /f
简单,直接,应该适用于所有地区。
出于我不理解的原因,重置“sShortDate”值立即生效
一个控制台窗口,但重置非常相似的“sTimeFormat”值不会生效
直到打开一个新的控制台窗口。但是,唯一可以更改的是分隔符-
数字的位置是固定的。同样,“HH”时间标记应该在前导零之前,但它没有。
幸运的是,变通方法很简单。