How get data/time independent from localization
Moderator: DosItHelp
Re: How get data/time independent from localization
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
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
Re: How get data/time independent from localization
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
The first post at that site describe the steps needed to get StdTime.exe and StdDate.exe programs.
Antonio
Re: How get data/time independent from localization
Nice !
..output is:
makecab is available in all versions of XP (folder system32).
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).
Re: How get data/time independent from localization
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
Re: How get data/time independent from localization
robocopy, some output examples:
week day and month name abbr. are in English, too
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
Re: How get data/time independent from localization
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
Re: How get data/time independent from localization
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
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%
Re: How get data/time independent from localization
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.
Re: How get data/time independent from localization
Have a look where I used NUL in the makecab line, carlos. There is no need for a temporary file in that switch.
Re: How get data/time independent from localization
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 )
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.
Re: How get data/time independent from localization
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.
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.
Re: How get data/time independent from localization
Code: Select all
set a=1|set b=2|set c=3
oh, this works
Code: Select all
set "a=1|set b=2|set c=3"
Re: How get data/time independent from localization
The date and time format will find in the registry.
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
@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
Re: How get data/time independent from localization
[quote="Endoro"] 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.
Code: Select all
set a=1|set b=2|set c=3
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.