How get data/time independent from localization

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

How get data/time independent from localization

#1 Post by darioit » 08 May 2013 01:54

Hello,

I have many script that get data and time in this way

Code: Select all

SET G=%DATE:~0,2%
SET M=%DATE:~3,2%
SET Y=%DATE:~6,4%
FOR /F "tokens=1 delims=."  %%G IN ('TIME /T') DO SET HH=%%G
SET MM=%TIME:~3,2%
SET SS=%TIME:~6,2%
SET DD=%Y%%M%%G%
SET DD0=%Y%-%M%-%G%
SET DD1=%G%/%M%/%Y%
SET ORA=%HH%%MM%
SET ORA1=%HH%.%MM%.%SS%


or
CALL :sub_routine file.txt

Code: Select all

:sub_routine
SET start=%time%
SET shh=%start:~0,2%
SET smm=%start:~3,2%
SET sss=%start:~6,2%
SET sms=%start:~9,2%
SET DateTime2=%date% %shh%.%smm%.%sss%.%sms%
FOR /f "tokens=1-7 delims=/. " %%a in ("%Datetime2%") do (set "H=0%%d" &call set "DateTime3=%%c-%%b-%%a %%H:~-2%%:%%e:%%f.%%g")
FOR /f "tokens=1-7 delims=/. " %%a in ("%~t1") do (set "H=0%%d" &call set "FileDateTime=%%c-%%b-%%a %%H:~-2%%:%%e:00.00")


I must migrate from Microsoft Server 2003 with Italy regional settings (18/04/2013 15:40:00) to Microsoft Server 2008 with english regional settings (4/18/2013 3:40:00 pm) and I don't want use anymore variable %date% and %time% to avoid this problem.

There is a "universal" way to get this information indipendent from localization? (also for get created date of a file)

Best Regards

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

Re: Get data/time independent from localization

#2 Post by foxidrive » 08 May 2013 02:51

Wmic should work - XP Pro and higher.


Code: Select all

:: timestamp YYYYMMDD_HHMMSS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set dt=%dt:~0,8%_%dt:~8,6%
echo %dt%
pause

Code: Select all

:: timestamp YYYY-MM-DD_HH-MM-SS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%
echo %dt%
pause

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

Re: How get data/time independent from localization

#3 Post by darioit » 08 May 2013 03:11

Doesn't works!! It's a loop that open many many cmd.exe !!

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

Re: How get data/time independent from localization

#4 Post by Endoro » 08 May 2013 03:15

do you have permission to write to the registry (HKCU)?

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

Re: How get data/time independent from localization

#5 Post by darioit » 08 May 2013 03:33

ok now works thanks, I fix the permission

but how can I get date creation of a specific file?
FOR /f "tokens=1-7 delims=/. " %%a in ("%~t1") do (set "H=0%%d" &call set "FileDateTime=%%c-%%b-%%a %%H:~-2%%:%%e:00.00")

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

Re: How get data/time independent from localization

#6 Post by Endoro » 08 May 2013 03:46

do you have "AM/PM" times? or like military --> 14:23

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

Re: How get data/time independent from localization

#7 Post by darioit » 08 May 2013 04:21

on new server is in English (United States) in this format
4/30/2013 2:05 PM

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

Re: How get data/time independent from localization

#8 Post by Endoro » 08 May 2013 04:29

The creation date doesn't work with "%%~ti", so use the "dir /tc" command, eg.

Code: Select all

@echo off &setlocal
for /f "delims=" %%i in ('dir /a-d /tc ^| findstr /b [0-9]') do (
   for /f "tokens=1-7*delims=/: " %%a in ("%%i") do (
      set "month=%%a"
      set "day=%%b"
      set "year=%%c"
      set "hour=%%d"
      set "min=%%e"
      set "AMPM=%%f"
      set "fname=%%h"
   )
   setlocal enabledelayedexpansion
   echo(!year!-!month!-!day! !hour!:!min! !AMPM! # !fname!
   endlocal
)

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

Re: How get data/time independent from localization

#9 Post by foxidrive » 08 May 2013 05:03

This is independent of region/locale I think. Executing wmic in a for in do is a problem when the file or path contains an & so this uses a temp file.



Code: Select all

:: file timestamp YYYYMMDD_HHMMSS
:: syntax: getfiledate.bat "c:\path\file.exe"
@echo off
set "file=%~1"
set "file=%file:\=\\%"
WMIC DATAFILE WHERE name="%file%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) do set dt=%%a
set dt=%dt:~0,8%_%dt:~8,6%
echo %dt%
del file.tmp
pause

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

Re: How get data/time independent from localization

#10 Post by darioit » 08 May 2013 07:04

many thanks WMIC is really great !!!

A last consideration, I have about 200 script that are running anytime this script to get time and date

It's correct to use only 1 bat to get date/time and passing variable to main script?

For example:

Myscript.bat

Code: Select all

call timestamp.bat
call datetimefile.bat "C:\myfile.txt"
echo %dt% %dt1%


timestamp.bat

Code: Select all

:: timestamp YYYY-MM-DD_HH-MM-SS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%
goto :eof


datetimefile.bat

Code: Select all

:: file timestamp YYYYMMDD_HHMMSS
:: syntax: getfiledate.bat "c:\path\file.exe"
@echo off
set "file=%~1"
set "file=%file:\=\\%"
WMIC DATAFILE WHERE name="%file%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) do set dt1=%%a
set dt1=%dt:~0,8%_%dt:~8,6%
del file.tmp
goto :eof


Or it's better put head to each script his own variable to get date/time?

Regards

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

Re: How get data/time independent from localization

#11 Post by foxidrive » 08 May 2013 07:47

darioit wrote:WMIC DATAFILE WHERE name="%file%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) do set dt1=%%a
set dt1=%dt1:~0,8%_%dt1:~8,6%


Note the two changes above where you changed the variable name but missed it in two spots.


If you ever contemplate changing the date and filestamp routines then keeping them in self contained batch files is a good idea.

The only time it could be an issue is if two or more batch files call the routine at the same time.

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

Re: How get data/time independent from localization

#12 Post by darioit » 08 May 2013 09:10

thanks for the reply I had already correct the error

So about the issue of two or more batch, it's better to call it with the command "CMD /C timestamp.bat" ?

Regards

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

Re: How get data/time independent from localization

#13 Post by miskox » 08 May 2013 13:14


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

Re: How get data/time independent from localization

#14 Post by carlos » 08 May 2013 13:34

I write this script in 2009, it only works in 32 bits windows (uses debug), but is other possibility:

Code: Select all

@Echo Off

:dateinfo
::last modification: 22/11/2009
setlocal enableextensions
for /f "tokens=2,6,8 delims== " %%a in (
'^(echo.e100 B4 2A CD 21 CC^&echo.g^&echo.q^)^|debug^|find "AX="'
) do set "df=%%a%%b%%c"
set /a "d=0x%df:~10,2%,m=0x%df:~8,2%,y=0x%df:~4,4%,wd=%df:~2,2%"
echo.day=%d%
echo.month=%m%
echo.year=%y%
echo.weekday=%wd%
exit /b


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

Re: How get data/time independent from localization

#15 Post by foxidrive » 08 May 2013 21:32

darioit wrote:So about the issue of two or more batch, it's better to call it with the command "CMD /C timestamp.bat" ?


This is fine: call timestamp.bat

Using cmd /c will not make any difference to multiple calls.

Post Reply