在 Windows 批处理文件中以 YYYYMMDD 格式获取日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14810544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Date in YYYYMMDD format in windows batch file
提问by user2061002
I need to get the date in YYYYMMDD format in batch file.
我需要在批处理文件中以 YYYYMMDD 格式获取日期。
I am doing this using :
我正在使用:
set mydate=%date:~6,4%%date:~3,2%%date:~0,2%
echo %mydate%
I need it to be consistent across system, even on changing the time settings.
我需要它在整个系统中保持一致,即使在更改时间设置时也是如此。
Please advise.
请指教。
回答by BDM
If, after reading the other questions and viewing the links mentioned in the comment sections, you still can't figure it out, read on.
如果在阅读其他问题并查看评论部分中提到的链接后,您仍然无法弄清楚,请继续阅读。
First of all, where you're going wrong is the offset.
首先,你出错的地方是偏移量。
It should look more like this...
它应该看起来更像这样......
set mydate=%date:~10,4%%date:~6,2%/%date:~4,2%
echo %mydate%
If the date was Tue 12/02/2013
then it would display it as 2013/02/12
. to remove the slashes, the code would look more like set mydate=%date:~10,4%%date:~7,2%%date:~4,2%
which would output 20130212
如果日期是Tue 12/02/2013
那么它将显示为2013/02/12
。要删除斜杠,代码看起来更像是set mydate=%date:~10,4%%date:~7,2%%date:~4,2%
输出20130212
And a hint for doing it in the future, if mydate
equals something like %date:~10,4%%date:~7,2%
or the like, you probably forgot a tilde (~).
并提示将来这样做,如果mydate
等于%date:~10,4%%date:~7,2%
或类似的东西,您可能忘记了波浪号 (~)。
回答by Gaurav Kolarkar_InfoCepts
You can try this ! This should work on windows machines.
你可以试试这个!这应该适用于 Windows 机器。
for /F "usebackq tokens=1,2,3 delims=-" %%I IN (`echo %date%`) do echo "%%I" "%%J" "%%K"