更新:现在是2016年,我将使用PowerShell,除非有一个真正令人信服的向后兼容的原因,特别是因为使用日期的区域设置问题。参见@npocmaka的https://stackoverflow.com/a/19799236/8479
什么是Windows命令行语句(s),我可以使用以我可以放入文件名的格式获取当前日期时间?
我想要一个.bat文件,它将一个目录压缩成一个归档文件,其中当前日期和时间作为名称的一部分,例如Code_2008-10-14_2257.zip。有什么简单的方法可以做到这一点,独立于机器的区域设置吗?
我真的不介意日期格式,理想情况下它是yyyy-mm-dd,但任何简单的都可以。
到目前为止,我已经得到了这个,在我的机器上给我Tue_10_14_2008_230050_91:
rem Get the datetime in a format that can go in a filename.
set _my_datetime=%date%_%time%
set _my_datetime=%_my_datetime: =_%
set _my_datetime=%_my_datetime::=%
set _my_datetime=%_my_datetime:/=_%
set _my_datetime=%_my_datetime:.=_%
rem Now use the timestamp by in a new ZIP file name.
"d:\Program Files\7-Zip\7z.exe" a -r Code_%_my_datetime%.zip Code
我可以接受这个,但它似乎有点笨重。理想情况下,它应该更简短,并具有前面提到的格式。
我用的是Windows Server 2003和Windows XP Professional。我不想安装额外的实用程序来实现这一点(尽管我知道有一些工具可以很好地格式化日期)。
我用vMax的批处理文件更改了答案,所以它也适用于荷兰语。
荷兰语(尽管我们是持久性的)在%date%、date/t和date中有一些更改,这些更改破坏了原始的批处理文件。
如果有些人也能检查其他Windows locale,并报告结果,那就太好了。
如果批处理文件在您的位置失败,那么请在命令提示符中包含这两个语句的输出:
回音:^ |日期
日期/ t
这是你应该从批处理文件中得到的输出示例:
C:\temp>set-date-cmd.bat
Today is Year: [2011] Month: [01] Day: [03]
20110103
以下是修改后的代码及其原因:
:: https://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-usi
:: Works on any NT/2k machine independent of regional date settings
::
:: 20110103 - adapted by jeroen@pluimers.com for Dutch locale
:: Dutch will get jj as year from echo:^|date, so the '%%c' trick does not work as it will fill 'jj', but we want 'yy'
:: luckily, all countries seem to have year at the end: http://en.wikipedia.org/wiki/Calendar_date
:: set '%%c'=%%k
:: set 'yy'=%%k
::
:: In addition, date will display the current date before the input prompt using dashes
:: in Dutch, but using slashes in English, so there will be two occurances of the outer loop in Dutch
:: and one occurence in English.
:: This skips the first iteration:
:: if "%%a" GEQ "A"
::
:: echo:^|date
:: Huidige datum: ma 03-01-2011
:: Voer de nieuwe datum in: (dd-mm-jj)
:: The current date is: Mon 01/03/2011
:: Enter the new date: (mm-dd-yy)
::
:: date/t
:: ma 03-01-2011
:: Mon 01/03/2011
::
:: The assumption in this batch-file is that echo:^|date will return the date format
:: using either mm and dd or dd and mm in the first two valid tokens on the second line, and the year as the last token.
::
:: The outer loop will get the right tokens, the inner loop assigns the variables depending on the tokens.
:: That will resolve the order of the tokens.
::
@ECHO off
set v_day=
set v_month=
set v_year=
SETLOCAL ENABLEEXTENSIONS
if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
::DEBUG echo toks=%toks%
for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
::DEBUG echo first token=%%a
if "%%a" GEQ "A" (
for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
set '%%a'=%%i
set '%%b'=%%j
set 'yy'=%%k
)
)
)
if %'yy'% LSS 100 set 'yy'=20%'yy'%
set Today=%'yy'%-%'mm'%-%'dd'%
ENDLOCAL & SET v_year=%'yy'%& SET v_month=%'mm'%& SET v_day=%'dd'%
ECHO Today is Year: [%V_Year%] Month: [%V_Month%] Day: [%V_Day%]
set datestring=%V_Year%%V_Month%%V_Day%
echo %datestring%
:EOF
——基珀斯
查看Windows批处理文件(.bat)以MMDDYYYY格式获取当前日期:
@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
echo %mydate%_%mytime%
如果你更喜欢24小时/军事格式的时间,你可以用这个替换第二个FOR行:
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
C: \ date.bat >
2008 - 10 - 14 - _0642
如果你想要日期独立于地区日/月顺序,你可以使用“WMIC os GET LocalDateTime”作为源,因为它是ISO顺序:
@echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
echo Local date is [%ldt%]
C: > test.cmd
本地日期为[2012-06-19 10:23:47.048]