How to get Windows batch date stamp that is locale independent? [duplicate]

我们两清 提交于 2020-08-15 10:43:19

问题


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

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

If the code is tuned for the English Windows, then it does not work in the Czech environment. How it should be implemented?


回答1:


Here is a method you can manipulate the output of 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%

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%


来源:https://stackoverflow.com/questions/56165593/how-to-get-windows-batch-date-stamp-that-is-locale-independent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!