如何在Windows命令行上获取当前的日期时间,以适合的格式使用文件名? [英] How to get current datetime on Windows command line, in a suitable format for using in a filename?

查看:332
本文介绍了如何在Windows命令行上获取当前的日期时间,以适合的格式使用文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:现在是2016年,我将使用powershell,除非有一个非常引人注目的向后兼容的原因,特别是因为使用的区域设置问题日期。请参阅@ npocmaka的 http://stackoverflow.com/a/19799236/8479






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



我想拥有一个.bat文件,将目录作为名称的一部分将当前日期和时间的目录卷入归档,例如 Code_2008-10- 14_2257.zip 。有没有什么简单的方法可以做到这一点,独立于机器的区域设置?



我不太了解日期格式,最好是yyyy-mm-dd,但任何简单的都很好。



到目前为止,我已经得到了,在我的机器上给了我 Tue_10_14_2008_230050_91

  rem以可以进入文件名的格式获取日期时间。 
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现在使用新的ZIP文件名中的时间戳。
d:\Program Files\7-Zip\7z.exea -r代码_%_ my_datetime%.zip代码

我可以这样生活,但似乎有点笨重。理想情况下,它会更加简洁,并具有前面提到的格式。



我使用的是Windows Server 2003和Windows XP专业版。我不想安装额外的实用程序来实现这一点(尽管我意识到有些会做出很好的日期格式化)。

解决方案

p>参见 Windows批处理文件(.bat)以MMDDYYYY格式获取当前日期

  @echo off 
对于/ ftokens = 2-4 delims = /%% a in('date / t')do(set mydate = %% c - %% a - %% b)
对于/ ftokens = 1-2 delims = /:%% a in('time / t')do(set mytime = %% a %% b)
echo%mydate%_%mytime%

如果您喜欢24小时/军事格式的时间,您可以用以下替换第二个FOR行: p>

 对于/ ftokens = 1-2 delims = /:%% a in(%TIME%)do mytime = %% a %% b)




C:>。 \\ date.bat

2008-10-14_0642


如果你想要他独立于区域日/月订单,可以使用WMIC os GET LocalDateTime作为源,因为它是ISO顺序:

  @echo off 
for / Fusebackq tokens = 1,2 delims ==%% i in(`wmic os get LocalDateTime / VALUE 2 ^&Nine`)如果'。%% 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本地日期是[%ldt%]
/ pre>


C:> test.cmd

本地日期是[2012-06-19 10:23 :47.048]


编辑:添加了您问题中最初要求的时间以及日期b $ b 编辑:使用WMIC添加与区域无关的变体


EDIT: 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. See @npocmaka's http://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?

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

@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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆