How get data/time independent from localization

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: How get data/time independent from localization

#16 Post by darioit » 09 May 2013 01:26

I try to run 3 different script that call same date/time subroutine but the variable date/time return to main bat are different each time.

I take a look to 3 script with sysinternal procexp.exe and I see that call correspond to "cmd /c"

From my point of view, this scripts works fine I don't see any problem

Regards

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

Re: How get data/time independent from localization

#17 Post by Aacini » 09 May 2013 20:11

You may also try my StdTime.exe and StdDate.exe auxiliary programs, that were written precisely to avoid locale date/time format problems. They works for both %DATE%/%TIME% variables and for file's date and time reported by DIR command or via %~T modifier of FOR command.

The first post at that site describe the steps needed to get StdTime.exe and StdDate.exe programs.

Antonio

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: How get data/time independent from localization

#18 Post by !k » 10 May 2013 10:22


Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: How get data/time independent from localization

#19 Post by Endoro » 10 May 2013 14:26

Nice :D !

Code: Select all

@echo off &setlocal
copy nul "%TEMP%\~.ddf" >nul
makecab /D RptFileName="%TEMP%\~.rpt" /D InfFileName="%TEMP%\~.inf" -f "%TEMP%\~.ddf">nul
for /f "tokens=3-7" %%a in ('type "%TEMP%\~.rpt"') do (
   if not defined current-date set "current-date=%%e-%%b-%%c"
   if not defined current-time set "current-time=%%d"
   if not defined weekday set "weekday=%%a"
)
del /q "%TEMP%\~.*"
echo %weekday% %current-date% %current-time%


..output is:

Code: Select all

Fri 2013-May-10 22:23:11


makecab is available in all versions of XP (folder system32).

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How get data/time independent from localization

#20 Post by foxidrive » 10 May 2013 22:46

Here's a simplification, and another option using Robocopy that was mentioned not too long ago.

Code: Select all

@echo off
pushd "%temp%"
makecab /D RptFileName=~.rpt /D InfFileName=~.inf /f nul >nul
for /f "tokens=3-7" %%a in ('find /i "makecab"^<~.rpt') do (
   set "current-date=%%e-%%b-%%c"
   set "current-time=%%d"
   set "weekday=%%a"
)
del ~.*
popd
echo %weekday% %current-date% %current-time%
pause



Code: Select all

@echo off
for /f "tokens=3-7 delims=, " %%a in ('robocopy /? ^|find /i "Started :"') do (
   set "current-date=%%d-%%c-%%b"
   set "current-time=%%e"
   set "weekday=%%a"
)
echo %weekday% %current-date% %current-time%
pause

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: How get data/time independent from localization

#21 Post by Endoro » 11 May 2013 01:45

robocopy, some output examples:

Code: Select all

>robocopy /?

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows     ::     Version XP010
-------------------------------------------------------------------------------

  Started : Sat May 11 09:37:14 2013

Code: Select all

>robocopy /?

-------------------------------------------------------------------------------
ROBOCOPY :: Robustes Dateikopieren für Windows
-------------------------------------------------------------------------------

Gestartet: Thu Feb 28 20:23:58 2013

week day and month name abbr. are in English, too

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How get data/time independent from localization

#22 Post by foxidrive » 11 May 2013 10:23

That is interesting. In Windows 8 I get the full dayname.

Code: Select all

z:\>robocopy /?

----------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows

----------------------------------------------------

  Started : Sunday, 12 May 2013 02:22:10

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: How get data/time independent from localization

#23 Post by carlos » 11 May 2013 12:11

I like the version using makecab, because robocopy is not in windows xp.
I do other simplification to the code, removing if defined and not using find command like foxidrive simplification, and removing setlocal

Code: Select all

@echo off
copy nul "%TEMP%\~.ddf" >nul
makecab /D RptFileName="%TEMP%\~.rpt" /D InfFileName="%TEMP%\~.inf" -f "%TEMP%\~.ddf">nul
set /p "current-date=" <"%TEMP%\~.rpt"
for /f "tokens=3-7" %%a in ("%current-date%") do (
  set "current-date=%%e-%%b-%%c"
  set "current-time=%%d"
  set "weekday=%%a"
)
del /q "%TEMP%\~.*"
echo %weekday% %current-date% %current-time%

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: How get data/time independent from localization

#24 Post by carlos » 11 May 2013 12:21

Optimized again. Because the months and weekday come in string format. I think is better have a one line variable with all the information.

Code: Select all

@echo off
copy nul "%TEMP%\~.ddf" >nul
makecab /D RptFileName="%TEMP%\~.rpt" /D InfFileName="%TEMP%\~.inf" -f "%TEMP%\~.ddf">nul
set /p "timestamp=" <"%TEMP%\~.rpt"
for /f "tokens=3-7" %%a in ("%timestamp%") do set "timestamp=%%e %%b %%c %%d %%a"
del /q "%TEMP%\~.*"
echo %timestamp%
Last edited by carlos on 11 May 2013 23:16, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How get data/time independent from localization

#25 Post by foxidrive » 11 May 2013 21:39

Have a look where I used NUL in the makecab line, carlos. There is no need for a temporary file in that switch.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: How get data/time independent from localization

#26 Post by carlos » 11 May 2013 23:22

foxidrive, after you mentions the use of -f nul. I test that it only works in win7, in xp, it fails. But, now with more test I found that the inf file is not needed, then you can specify nul in it. This is a new optimization. I change the way of creation of empty file (maybe milliseconds more speedy :P)

Code: Select all

@Echo Off
Keys List >"%TEMP%\~.ddf"
Makecab /D RptFileName="%TEMP%\~.rpt" /D InfFileName=Nul /F "%TEMP%\~.ddf" >Nul
Set /P "timestamp=" <"%TEMP%\~.rpt"
For /F "tokens=3-7" %%a In ("%timestamp%") Do Set "timestamp=%%e %%b %%c %%d %%a"
Del /Q "%TEMP%\~.*"
Echo %timestamp%
Last edited by carlos on 12 May 2013 11:27, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How get data/time independent from localization

#27 Post by foxidrive » 12 May 2013 01:41

Nice one, carlos.

Back in the days of Batpower we used to have a competition to see who could create the smallest bat file to do a task. That was fun.

You'd see things like set a=1|set b=2|set c=3 because the pipes shaved off one byte per line. :)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: How get data/time independent from localization

#28 Post by Endoro » 12 May 2013 03:53

Code: Select all

set a=1|set b=2|set c=3
this doesn't set anything?

oh, this works

Code: Select all

set "a=1|set b=2|set c=3"

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: How get data/time independent from localization

#29 Post by trebor68 » 12 May 2013 06:39

The date and time format will find in the registry.

Code: Select all

@echo off
rem Source: http://technet.microsoft.com/en-us/library/cc978632.aspx

for %%a in (sDate iDate sLongDate sShortDate sTime iTime iTLZero s1159 s2359) do (
  for /f "tokens=2*" %%r in ('REG QUERY "HKCU\Control Panel\International" /v %%a') do (
    set %%a=%%s
  )
)

echo sDate      : "%sDate%"
echo iDate      : "%iDate%"
echo sLongDate  : "%sLongDate%"
echo sShortDate : "%sShortDate%"
echo sTime      : "%sTime%"
echo iTime      : "%iTime%"
echo iTLZero    : "%iTLZero%"
echo s1159      : "%s1159%"
echo s2359      : "%s2359%"
echo.
if %iDate% equ 0 echo short date: mm%sDate%dd%sDate%yy
if %iDate% equ 1 echo short date: dd%sDate%mm%sDate%yy
if %iDate% equ 2 echo short date: yy%sDate%mm%sDate%dd
echo.
if %iTLZero% equ 0 echo Time format:   h%sTime%mm%sTime%ss tt
if %iTLZero% equ 1 echo Time format:  0h%sTime%mm%sTime%ss tt  when single-digit hour
echo The "tt" is value of "s1159" or "s2359"
echo.
if %iTime% equ 0 echo Time with 12-hour clock
if %iTime% equ 1 echo Time with 24-hour clock



The day format have different value:
d - day; example 8, 9, 10, 11
dd - day; example 08, 09, 10, 11
ddd - day of the week; Mon, Thu, Wed
dddd - day of the week; Monday, Friday
M - month; example 8, 9, 10, 11
MM - month; example 08, 09, 10, 11
MMM - month; Oct, Nov
MMMM - month; October, November
yy - year; example 13
yyyy - year; example 2013


Here the results from my computer (Germany):

Code: Select all

sDate      : "."
iDate      : "1"
sLongDate  : "dddd, d. MMMM yyyy"
sShortDate : "dd.MM.yyyy"
sTime      : ":"
iTime      : "1"
iTLZero    : "1"
s1159      : ""
s2359      : ""

short date: dd.mm.yy

Time format:  0h:mm:ss tt  when single-digit hour
The "tt" is value of "s1159" or "s2359"

Time with 24-hour clock

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How get data/time independent from localization

#30 Post by foxidrive » 12 May 2013 07:29

[quote="Endoro"]

Code: Select all

set a=1|set b=2|set c=3
this doesn't set anything?

It works in MSdos V6.22

It doesn't work in Win 8 either - and putting quotes around it sets the single variable, which is not the same thing.

Post Reply