在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

当前回答

在这种情况下,使用简单、标准的编程方法: 与其花费大量精力解析未知实体,不如简单地保存当前配置,将其重置为已知状态,提取信息,然后恢复原始状态。只使用标准的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”时间标记应该在前导零之前,但它没有。 幸运的是,变通方法很简单。

其他回答

当我需要一个日期/时间字符串时,我通常这样做:

set dt=%DATE:~6,4%_%DATE:~3,2%_%DATE:~0,2%__%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
set dt=%dt: =0%

这是为德国日期/时间格式(dd.mm。yyyy hh: mm: ss)。基本上,我将子字符串连接起来,最后将所有空格替换为零。

结果字符串的格式为:yyyy_mm_dd__hh_mm_ss


子字符串如何工作的简短解释:

%VARIABLE:~num_chars_to_skip,num_chars_to_keep%

因此,要想从“29.03.2018”这样的日期中得到年份,可以使用:

%DATE:~6,4%
       ^-----skip 6 characters
         ^---keep 4 characters 

我是这样做的:

REM Generate FileName from date and time in format YYYYMMTTHHMM

Time /T > Time.dat
set /P ftime= < Time.dat

set FileName=LogFile%date:~6%%date:~3,2%%date:~0,2%%ftime:~0,2%%ftime:~3,2%.log

echo %FileName%

LogFile201310170928.log

set hourstr = %time:~0,2%
if "%time:~0,1%"==" " (set hourstr=0%time:~1,1%)
set datetimestr=%date:~0,4%%date:~5,2%%date:~8,2%-%hourstr%%time:~3,2%%time:~6,2%
for /f "tokens=1-4 delims=/ " %%I in ("%DATE%") do set curdate=%%K_%%J_%%I
for /f "tokens=1-4 delims=:," %%I in ("%TIME: =0%") do set curtime=%%I_%%J_%%K
echo %curdate%__%curtime%

如果您的系统使用另一个替换delims符号(用于切片到%%I,%%J…) 如果您的系统使用另一个D/M/Y订单,也可以编辑curdate

在这种情况下,使用简单、标准的编程方法: 与其花费大量精力解析未知实体,不如简单地保存当前配置,将其重置为已知状态,提取信息,然后恢复原始状态。只使用标准的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”时间标记应该在前导零之前,但它没有。 幸运的是,变通方法很简单。