如何获得在Windows命令行当前日期时间,以合适的格式用于使用在一个文件名? [英] How to get current datetime on Windows command line, in a suitable format for using in a filename?
问题描述
什么是Windows命令行声明(S)我可以用它来获得的格式当前日期时间,我可以放到一个文件名?
我想有一个拉链一个目录与当前的日期和时间作为名称的一部分存档,例如.bat文件, code_2008-10-14_2257.zip
。有没有简单的方法,我可以做到这一点,独立于计算机的区域设置?
我真的不介意的日期格式,最好这将会是YYYY-MM-DD,但任何事情简单的罚款。
到目前为止,我已经得到了这一点,这在我的机器上给了我 Tue_10_14_2008_230050_91
:
REM获取,可以在文件名中去格式的日期时间。
集_my_datetime =%日期%_%时间%
设置_my_datetime =%_ my_datetime:= _%
设置_my_datetime =%_ my_datetime :: =%
设置_my_datetime =%_ my_datetime:/ = _%
设置_my_datetime =%_ my_datetime:= _%REM现在在新的ZIP文件名中使用的时间戳。
D:\\ Program Files文件\\的7-Zip \\ 7z.exe一-r code _ _%%my_datetime .ZIP code
我可以用这个生活,但似乎有点笨重。理想情况下它会是简短并有格式前面提到的。
我使用Windows Server 2003和Windows  XP专业版。我不想安装额外的工具来实现这一点(虽然我知道有一些会做很好的日期格式)。
请参阅Windows批处理文件(.bat),以获得在MMDDYYYY格式的当前日期:
关闭@echo
FOR / F令牌= 2-4 delims = /%%一中(日期/ T')DO(设置数值指明MyDate = %%Ç - 一个%% - %% B)
FOR / F令牌= 1-2 delims = /:%%一个在('时间/ T')做(设置数值指明MyTime = %% %%一B)
回声%数值指明MyDate%_%数值指明MyTime%
如果您preFER在24小时/军事格式的时间,你可以替换行的第二个与此:
FOR / F令牌= 1-2 delims = /:%%一中(%TIME%),做(设置数值指明MyTime = %% %%一B)
C:> \\ date.bat
2008-10-14_0642
块引用>如果您想要的日期独立于区域日/月秩序,可以用WMIC OS GET LocalDateTime作为来源,因为它在ISO顺序为:
关闭@echo
FOR / F令牌有usebackq = 1,2 delims ==%% i的(`WMIC OS获得LocalDateTime /值2 ^> NUL`)做,如果=='%%我。''LocalDateTime。集LDT = %%Ĵ
设置LDT =%LDT:0,4〜% - %LDT:〜4.2% - %LDT:6.2〜%% LDT:〜8.2%:%LDT:10,2〜%:%LDT: 〜12,6%
回声本地日期是[%LDT%]
C:>
当地的日期为[2012-06-19 10:23:47.048]
块引用>修改:增加了时间和日期作为你的问题最初申请
修改:增加使用WMIC区域独立变化What's a Windows command line statement(s) I can use to get the current datetime in a format that I can put into a filename?
I want to have a .bat file that zips up a directory into an archive with the current date and time as part of the name, for example,
Code_2008-10-14_2257.zip
. Is there any easy way I can do this, independent of the regional settings of the machine?I don't really mind about the date format, ideally it'd be yyyy-mm-dd, but anything simple is fine.
So far I've got this, which on my machine gives me
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
I can live with this, but it seems a bit clunky. Ideally it'd be briefer and have the format mentioned earlier.
I'm using Windows Server 2003 and Windows XP Professional. I don't want to install additional utilities to achieve this (although I realise there are some that will do nice date formatting).
解决方案See Windows Batch File (.bat) to get current date in MMDDYYYY format.:
@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%
If you prefer the time in 24hr/military format, you can replace the second FOR line with this:
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
C:> .\date.bat
2008-10-14_0642If you want the date independently of the region day/month order, you can use "WMIC os GET LocalDateTime" as a source, since it's in ISO order:
@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
Local date is [2012-06-19 10:23:47.048]EDIT: Added the time as well as the date as originally requested in your question EDIT: Added a region-independent variation using WMIC
这篇关于如何获得在Windows命令行当前日期时间,以合适的格式用于使用在一个文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!