Windows批处理:格式化的日期到变量 [英] Windows batch: formatted date into variable
本文介绍了Windows批处理:格式化的日期到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何保存在YYYY-MM-DD格式的当前日期插入到Windows中的某些变量.bat文件?
Unix外壳模拟:
How to save current date in YYYY-MM-DD format into some variable in Windows .bat file? Unix shell analogue:
today=`date +%F`
echo $today
感谢
推荐答案
您可以用获取当前日期的语言环境无关的方式
You can get the current date in a locale-agnostic way using
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
然后就可以使用提取子的各个部分:
Then you can extract the individual parts using substrings:
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
另一种方式,你在哪里得到包含各个部分的变量,将是:
Another way, where you get variables that contain the individual parts, would be:
for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x
set today=%Year%-%Month%-%Day%
比摆弄子在污染你的变量命名空间为代价更好。
Much nicer than fiddling with substrings, at the expense of polluting your variable namespace.
如果您需要UTC而不是本地时间,命令或多或少是一样的:
If you need UTC instead of local time, the command is more or less the same:
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do set %%x
set today=%Year%-%Month%-%Day%
这篇关于Windows批处理:格式化的日期到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文