问题描述
在Windows批处理文件中,能否以某种与位置无关的形式获取日期文本,以获取字符串形式的与位置无关的日期戳?为了使问题更清楚...在捷克语的Windows中,所需的代码如下所示:
In a Windows batch file, is it possible to get the text of date in some location independent form to get the location independent date stamp as a string? To make the question clear... In the Czech Windows, the wanted code looks like this:
d:\>date /t
čt 16. 05. 2019
d:\>echo %DATE:~-4%%DATE:~-8,2%%DATE:~-12,2%
20190516
但是,在英文Windows中,出于明显的原因,相同的代码会返回错误的结果:
However, in the English Windows the same code returns bad results for obvious reasons:
d:\>date /t
Thu 05/16/2019
d:\>echo %DATE:~-4%%DATE:~-8,2%%DATE:~-12,2%
2019/1u
如果代码已针对英语Windows进行了调整,则它在捷克语环境中不起作用.应该如何实施?
If the code is tuned for the English Windows, then it does not work in the Czech environment. How it should be implemented?
推荐答案
这是您可以操纵wmic os get LocalDateTime /VALUE
@echo off
for /f "tokens=1,2 delims==" %%i in ('wmic os get LocalDateTime /VALUE ^>nul 2^>^&1') do (
if ".%%i."==".LocalDateTime." set mydate=%%j
)
set mydate=%mydate:~0,4%/%mydate:~4,2%/%mydate:~6,2% %mydate:~8,2%:%mydate:~10,2%:%mydate:~12,6%
echo %mydate%
,并且您似乎希望使用YYYYMMDD
的特定格式,并且不包括时间:
and in the specific format you seem to want it YYYYMMDD
and excluding time:
@echo off
for /f "tokens=1,2 delims==" %%i in ('wmic os get LocalDateTime /VALUE ^>nul 2^>^&1') do (
if ".%%i."==".LocalDateTime." set mydate=%%j
)
set mydate=%mydate:~0,4%%mydate:~4,2%%mydate:~6,2%
echo %mydate%
这篇关于如何获取与语言环境无关的Windows批处理日期戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!