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: 4678
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: 4678
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: 4678
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: 4678
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

miskox
Posts: 631
Joined: 28 Jun 2010 03:46

Re: date and time format inconsistencies

#9 Post by miskox » 09 Oct 2024 02:26

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:

Code: Select all

findstr /L ". ,"
Or even better:

Code: Select all

for /f %%i in ('WMIC OS GET LocalDateTime /value ^| find "="') do set "%%i"
Saso

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: date and time format inconsistencies

#10 Post by Squashman » 09 Oct 2024 13:12

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: date and time format inconsistencies

#11 Post by Aacini » 09 Oct 2024 16:07

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.

miskox
Posts: 631
Joined: 28 Jun 2010 03:46

Re: date and time format inconsistencies

#12 Post by miskox » 10 Oct 2024 03:58

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

Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: date and time format inconsistencies

#13 Post by Sponge Belly » 17 Oct 2024 10:52

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! 8)

- SB

miskox
Posts: 631
Joined: 28 Jun 2010 03:46

Re: date and time format inconsistencies

#14 Post by miskox » 17 Oct 2024 22:27

Very good post at SO!

Thanks.

Saso

Post Reply