在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

当前回答

还有另一种简单的方法:

set HH=%time:~0,2%
if %HH% LEQ 9 (
set HH=%time:~1,1%
)

其他回答

我今天遇到了这个问题,并解决了它:

SET LOGTIME=%TIME: =0%

它用0代替空格,基本上是零垫小时。

经过一些快速搜索,我没有发现它是否需要命令扩展(仍然使用SETLOCAL DISABLEEXTENSIONS)。

我真的是批处理文件的新手,这是我的代码!!(我不知道为什么,但我不能结合日期/t和时间/t在一起,我不能直接使用%date%和%time%没有变量…)

@ECHO OFF
set ldt=%date% %time%
echo %ldt%>> logs.txt
EXIT

它在某种程度上是从其他人那里重用的(问题是获得一个格式化的时间作为文件名)。

@ECHO OFF
: Sets the proper date and time stamp with 24Hr Time for log file naming
: convention ('YYYYMMDD_HHMMSS')

: Scrapes the characters out of their expected permissions in the date/time
: environment variables.

: Expects a date format of '____MM_DD_YYYY'
: Expects a time format of 'HH:MM:SS' or ' H:MM:SS'

SET HOUR=%time:~0,2%
SET dtStamp9=%date:~-4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2% 
SET dtStamp24=%date:~-4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%

if "%HOUR:~0,1%" == " " (SET dtStamp=%dtStamp9%) else (SET dtStamp=%dtStamp24%)

ECHO %dtStamp%

PAUSE

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

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 
:: =============================================================
:: Batch file to display Date and Time seprated by undescore.
:: =============================================================
:: Read the system date.
:: =============================================================
@SET MyDate=%DATE%
@SET MyDate=%MyDate:/=:%
@SET MyDate=%MyDate:-=:%
@SET MyDate=%MyDate: =:%
@SET MyDate=%MyDate:\=:%
@SET MyDate=%MyDate::=_%
:: =============================================================
:: Read the system time.
:: =============================================================
@SET MyTime=%TIME%
@SET MyTime=%MyTime: =0%
@SET MyTime=%MyTime:.=:%
@SET MyTime=%MyTime::=_%
:: =============================================================
:: Build the DateTime string.
:: =============================================================
@SET DateTime=%MyDate%_%MyTime%
:: =============================================================
:: Display the Date and Time as it is now.
:: =============================================================
@ECHO MyDate="%MyDate%" MyTime="%MyTime%" DateTime="%DateTime%"
:: =============================================================
:: Wait before close.
:: =============================================================
@PAUSE
:: =============================================================