Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

windows - 如何在Windows命令行上以合适的格式获取当前日期/时间以在文件/文件夹名称中使用?(How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?)

Update: Now that it's 2016 I'd use PowerShell for this unless there's a really compelling backwards-compatible reason for it, particularly because of the regional settings issue with using date .

(更新:现在是2016年,除非有确实令人信服的向后兼容原因,否则我将使用PowerShell,特别是由于使用date的区域设置问题。)

See @npocmaka's https://stackoverflow.com/a/19799236/8479

(参见@npocmaka的https://stackoverflow.com/a/19799236/8479)


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?

(什么是Windows命令行语句,我可以用可以放入文件名的格式来获取当前日期时间?)

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 .

(我想要一个.bat文件,该文件将目录压缩到一个存档中,并将当前日期和时间作为名称的一部分,例如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.

(我不太在乎日期格式,理想情况下是yyyy-mm-dd,但是任何简单的方法都可以。)

So far I've got this, which on my machine gives me Tue_10_14_2008_230050_91 :

(到目前为止,我已经知道了,在我的机器上这给了我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 Files7-Zip7z.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.

(我正在使用Windows Server 2003和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).

(我不想安装其他实用程序来实现此目的(尽管我知道有些实用程序可以很好地格式化日期)。)

  ask by Rory translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

See Windows Batch File (.bat) to get current date in MMDDYYYY format :

(请参阅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%

If you prefer the time in 24 hour/military format, you can replace the second FOR line with this:

(如果您希望使用24小时/军事格式的时间,则可以使用以下命令替换第二条FOR行:)

For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)

C:> .\date.bat

(C:>。\ date.bat)
2008-10-14_0642

(2008-10-14_0642)

If 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:

(如果您希望日期与区域日/月顺序无关,则可以使用“ 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

(C:> test.cmd)
Local date is [2012-06-19 10:23:47.048]

(当地日期是[2012-06-19 10:23:47.048])


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...