如何将 YYYY-MM-DD 格式的当前日期保存到 Windows .bat 文件中的某个变量中?
Unix shell 模拟:
today=`date +%F`
echo $today
最佳答案
您可以使用与语言环境无关的方式获取当前日期
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
然后您可以使用子字符串提取各个部分:
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
另一种获取包含各个部分的变量的方法是:
for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x
set today=%Year%-%Month%-%Day%
比摆弄子字符串好得多,代价是污染你的变量命名空间。
如果您需要 UTC 而不是本地时间,命令或多或少是相同的:
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do set %%x
set today=%Year%-%Month%-%Day%
关于Windows 批处理 : formatted date into variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10945572/