date and time format inconsistencies

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mrpaulc
Posts: 4
Joined: 09 Nov 2019 09:44

date and time format inconsistencies

#1 Post by mrpaulc » 09 Nov 2019 11:00

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: date and time format inconsistencies

#2 Post by aGerman » 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.

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

mrpaulc
Posts: 4
Joined: 09 Nov 2019 09:44

Re: date and time format inconsistencies

#3 Post by mrpaulc » 09 Nov 2019 12:29

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: date and time format inconsistencies

#4 Post by aGerman » 09 Nov 2019 12:47

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

mrpaulc
Posts: 4
Joined: 09 Nov 2019 09:44

Re: date and time format inconsistencies

#5 Post by mrpaulc » 09 Nov 2019 13:16

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: date and time format inconsistencies

#6 Post by aGerman » 09 Nov 2019 14:50

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

mrpaulc
Posts: 4
Joined: 09 Nov 2019 09:44

Re: date and time format inconsistencies

#7 Post by mrpaulc » 09 Nov 2019 15:11

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: date and time format inconsistencies

#8 Post by aGerman » 09 Nov 2019 15:26

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

Post Reply