Page 1 of 1
date and time format inconsistencies
Posted: 09 Nov 2019 11:00
by mrpaulc
why does this batch file ...
Code: Select all
@ echo off
cls
for /f "tokens=3" %%G in ('reg query ^"HKEY_CURRENT_USER\Control Panel\International^" /v sTimeFormat ^| find ^"REG_SZ^"') do (
set time_format=%%G)
echo %time_format%
echo time format in registry
echo.
time/t
echo time from time/t command
echo.
echo %time%
echo time environment variable
echo.
echo ~ ~ ~
echo.
for /f "tokens=3" %%H in ('reg query ^"HKEY_CURRENT_USER\Control Panel\International^" /v sShortDate ^| find ^"REG_SZ^"') do (
set date_format=%%H)
echo %date_format%
echo date format in registry
echo.
date/t
echo date from date/t command
echo.
echo %date%
echo date environment variable
echo.
echo.
echo.
pause
... produce this output?
Code: Select all
h:mm:ss
time format in registry
09:46 AM
time from time/t command
9:46:11.62
time environment variable
~ ~ ~
M/d/yyyy
date format in registry
Sat 11/09/2019
date from date/t command
Sat 11/09/2019
date environment variable
Press any key to continue . . .
I do not understand the discrepancies in formats
time/t outputs a leading zero for hours and ends with am/pm
time variable outputs hundredths of seconds
date/t and date variable outputs day of the week
how could I reliably reformat the time and date despite the locale?
where is cmd.exe getting the format from?
Re: date and time format inconsistencies
Posted: 09 Nov 2019 11:59
by aGerman
We have large threads about date and time formatting in this forum. They point out that not even the values of the registry keys are reliable to find out how the cmd formats the strings. To cut a long story short - use WMIC.
Code: Select all
@echo off &setlocal
for /f %%i in ('WMIC OS GET LocalDateTime /value') do for /f %%j in ("%%i") do set "%%j"
echo %LocalDateTime%
pause
Just use string manipulation to get the values
Code: Select all
echo year %LocalDateTime:~0,4%
echo month %LocalDateTime:~4,2%
echo day %LocalDateTime:~6,2%
Steffen
Re: date and time format inconsistencies
Posted: 09 Nov 2019 12:29
by mrpaulc
aGerman wrote: ↑09 Nov 2019 11:59
We have large threads about date and time formatting in this forum. They point out that not even the values of the registry keys are reliable to find out how the cmd formats the strings. To cut a long story short - use WMIC
I would prefer to do this without any external tools
where is cmd.exe getting the format from?
Re: date and time format inconsistencies
Posted: 09 Nov 2019 12:47
by aGerman
What do you mean? wmic.exe belongs to the Windows command line tools just as reg.exe does. If using reg.exe is okay for you, what's wrong with wmic.exe?
Steffen
Re: date and time format inconsistencies
Posted: 09 Nov 2019 13:16
by mrpaulc
my use of reg.exe is for demonstration purposes. I would still like to understand how cmd.exe formats time and date. if it's not pulling this information from the registry, then where? and why does time/t and %time% give different results?
Re: date and time format inconsistencies
Posted: 09 Nov 2019 14:50
by aGerman
I assume the format specification is either hard-coded in the executable or (since it is locale-specific) in a .mui file.
However, how could you ever find this information in a binary file if you still want to use only built-in functions of the cmd?
Steffen
Re: date and time format inconsistencies
Posted: 09 Nov 2019 15:11
by mrpaulc
maybe it is hard coded. and maybe someone knows of a list of formats for locales. still puzzles me why %date% and date/t are the same while %time% and time/t are different. and maybe I will end up using wmic
Re: date and time format inconsistencies
Posted: 09 Nov 2019 15:26
by aGerman
mrpaulc wrote: ↑09 Nov 2019 15:11
and maybe someone knows of a list of formats for locales.
It's dependent on Windows versions, too. There is no value in gathering a list of formats if you are already able to get the information locale-independent IMHO.
Steffen
Re: date and time format inconsistencies
Posted: 09 Oct 2024 02:26
by miskox
aGerman wrote: ↑09 Nov 2019 11:59
Code: Select all
@echo off &setlocal
for /f %%i in ('WMIC OS GET LocalDateTime /value') do for /f %%j in ("%%i") do set "%%j"
echo %LocalDateTime%
pause
A little change to the code:
Code: Select all
for /f %%i in ('WMIC OS GET LocalDateTime /value ^| find "."') do set "%%i"
Is it possible that comma would be displayed instead of a dot (regional settings?) ? In this case this could be used:
Or even better:
Code: Select all
for /f %%i in ('WMIC OS GET LocalDateTime /value ^| find "="') do set "%%i"
Saso
Re: date and time format inconsistencies
Posted: 09 Oct 2024 13:12
by Squashman
Also realize that Microsoft has put wmic.exe on its deprecation list. It will be removed from Windows in the near future.
That being said you can call out to powershell from a batch file to get the date and time.
Code: Select all
FOR /F "usebackq delims=" %%G IN (`powershell "(Get-Date).ToString('yyyy-MM-dd-hh-mm-ss')"`) DO set dt=%%G
Re: date and time format inconsistencies
Posted: 09 Oct 2024 16:07
by Aacini
You can also use my
wmiClass.bat program that access the WMI Classes in an efficient way via JScript code. For example:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for /F %%a in ('wmiClass OS LocalDateTime') do set "%%a"
echo LocalDateTime=%LocalDateTime%
for %%a in ("Year=0,4" "Month=4,2" "Day=6,2" "Hour=8,2" "Minute=10,2" "Second=12,2") do (
for /F "tokens=1,2 delims==" %%b in (%%a) do (
set "%%b=!LocalDateTime:~%%c!"
)
)
echo %Year%/%Month%/%Day% @ %Hour%:%Minute%:%Second%
Output:
Code: Select all
LocalDateTime=20241009160244.601000-360
2024/10/09 @ 16:02:44
Antonio
PS - Just a note to indicate that JScript language is an inherent part of Windows (the same as PowerShell or wmic.exe), so it comes preinstalled in every modern version of Windows.
Re: date and time format inconsistencies
Posted: 10 Oct 2024 03:58
by miskox
Thank you both.
Is it really possible for MS to remove WMIC support? I mean...there could be millions of batch scripts to fail in the world.
I read a blog sometime ago (can't find it) that MS will never fix bugs in cmd.exe because of this reason. If I remember correctly MS did something similar in the past and it was no good response from some enterprisec (fortune 500 companies).
Saso
Re: date and time format inconsistencies
Posted: 17 Oct 2024 10:52
by Sponge Belly
Hello All,
npocmaka outlines a locale-independent method for capturing the current date and time using makeCab in the second answer to this
SO Question.
I don’t know why it isn’t used more widely.
HTH!
- SB
Re: date and time format inconsistencies
Posted: 17 Oct 2024 22:27
by miskox
Very good post at SO!
Thanks.
Saso