How get data/time independent from localization

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: How get data/time independent from localization

#31 Post by trebor68 » 12 May 2013 10:45

Why without the variable %DATE% and %TIME%?

Here a simple solution:

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS
set datetime=%date% %time%
rem Change the next line to the correct seperator; here for Germany
set datetime=%datetime:.= %
set datetime=%datetime::= %
call :TimeCheck %datetime%
goto :eof

:TimeCheck
if %4 geq 12 (set /a std=%4 - 12) & (set ampm=PM) else (set std=%4) & set ampm=AM
set newtime=%2/%1/%3 %std%:%5:%6,%7 %ampm%
echo %newtime%


The result is:
05/12/2013 6:43:51,90 PM

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

Re: How get data/time independent from localization

#32 Post by Endoro » 12 May 2013 11:07

%date% and %time% are not independent from regional & local settings
furthermore it depends on every single user

example: set your date format to dd.mm.

Code: Select all

C:\>echo %date%
12.05.

.. you get the same date from "dir" and there is no possibility to get a valid current year elswhere in pure XP batch (except makecab)

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

Re: How get data/time independent from localization

#33 Post by carlos » 12 May 2013 11:46

thanks foxidrive.
I want write Keys List but mistaked I write Break List. I updated my last one optimization for use Keys List as my original idea.

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

Re: How get data/time independent from localization

#34 Post by foxidrive » 12 May 2013 16:01

I didn't know of the keys command, carlos, (and just assumed that break worked). Thanks also for testing the nul with /f and finding it doesn't work on XP - useful to know.

It's still not a complete solution as the month has to be calculated, but that's not difficult.

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

Re: How get data/time independent from localization

#35 Post by carlos » 12 May 2013 17:02

foxidrive. I separate all the information in variables:
note: (the -100 is for avoid problems with octal notation with the number 09)

Code: Select all

@Echo Off
Set /A "Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12"
Set /A "Mon=1,Tue=2,Wed=3,Thu=4,Fri=5,Sat=6,Sun=7"
Type Nul >"%TEMP%\~.ddf"
Makecab /D RptFileName="%TEMP%\~.rpt" /D InfFileName=Nul /F "%TEMP%\~.ddf" >Nul
Set /P "timestamp=" <"%TEMP%\~.rpt"
For /F "tokens=3-9 delims=: " %%a In ("%timestamp%") Do (
Set /A "year=%%g,month=%%b,day=1%%c-100,weekday=%%a"
Set /A "hour=1%%d-100,minute=1%%e-100,second=1%%f-100"
)
Del /Q "%TEMP%\~.*"
Echo year=%year% month=%month% day=%day% weekday=%weekday%
Echo hour=%hour% minute=%minute% second=%second%
Last edited by carlos on 12 May 2013 19:55, edited 1 time in total.

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

Re: How get data/time independent from localization

#36 Post by foxidrive » 12 May 2013 18:02

That's getting to be a handy solution. Does it behave the same in a system using AM/PM time? Mine is set to 24 hour time.


BTW I get this in Win 8 - is it meant to pad the month/hour/minute etc single digits with a leading zero?

Code: Select all

year=2013 month=5 day=13 weekday=1
hour=10 minute=0 second=8



One other point I would bring up - 'keys list' works fine but I would prefer to use 'type nul' which has been a fairly standard way of creating a zero byte file - to avoid user confusion when they read the code.

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

Re: How get data/time independent from localization

#37 Post by carlos » 12 May 2013 19:57

good point foxidrive. I update the last code for implement your suggestion: Replaced 'Break List' by 'Type Nul' for avoid confusions.

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

Re: How get data/time independent from localization

#38 Post by Aacini » 12 May 2013 20:09

Code: Select all

HKCU\Control Panel\International subkey - Regional and Language Options
http://technet.microsoft.com/en-us/library/cc784842(v=ws.10).aspx

Name            Description       Standard value

sDecimal        decimal point     .

sDate           date separator    /
iDate           date order        0=mm/dd/yy, 1=dd/mm/yy, 2=yy/mm/dd (short date)
sShortDate      short format      m/d/yy = 6/5/69
                                  ddd - day of the week; Mon, Thu, Wed
 
sTime           time separator    :
sTimeFormat     time format       h:mm:ss tt = 8:20:00 AM (tt is s1159 or s2359)
s1159           AM string         a.m.
s2359           PM string         p.m.
iTime           time format       0=12-hour clock, 1=24-hour clock

Previous values are used by the Batch file below:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

:GetDateTime [date time]

rem Separate the date/time data in its components: YYYY, MM, DD and H, M, S
rem independent of locale regional settings.
rem MM, DD, and all time fields will have a left zero if the value is less than 10

rem If no parameter is given, current date and time are used.
rem Otherwise, the parameter must have the result of ~T modifier in FOR variable.

rem Load the International subkey - Regional and Language Options
for /F "tokens=1,2*" %%a in ('REG QUERY "HKCU\Control Panel\International"') do (
   if "%%b" equ "REG_SZ" (
      set %%a=%%c
   )
)
set dateOrder[0]=MM DD YYYY
set dateOrder[1]=DD MM YYYY
set dateOrder[2]=YYYY MM DD

if "%2" neq "" goto FileDateTime

rem Get current time
for /F "tokens=1-3 delims=%sTime%%sDecimal%" %%a in ("%time%") do (
   set H=%%a& set M=%%b& set S=%%c
)
if "%H:~1,1%" equ "" set H=0%H%

rem Get current date
set dateTokens=1-3
for /F %%a in ("%sShortDate%") do if "%%a" equ "ddd" set dateTokens=2-4
for /F "tokens=%dateTokens% delims=%sDate% " %%a in ("%date%") do (
   for /F "tokens=1-3" %%x in ("!dateOrder[%iDate%]!") do (
      set %%x=%%a& set %%y=%%b& set %%z=%%c
   )
)
if "%DD:~1,1%" equ "" set DD=0%DD%
if "%MM:~1,1%" equ "" set MM=0%MM%
if "%YYYY:~2,2%" equ "" set YYYY=20%YYYY%

goto ShowResult

:FileDateTime

rem Get file time
for /F "tokens=1-2 delims=%sTime%%sDecimal%" %%a in ("%2") do (
   set /A H=10%%a %% 100 & set M=%%b
)
set S=00
if %iTime% equ 0 (
   if "%3" equ "%s2359%" (
      if %H% lss 12 set /A H+=12
   ) else (
      if %H% equ 12 set H=0
   )
)
if "%H:~1,1%" equ "" set H=0%H%

rem Get file date
for /F "tokens=1-3 delims=%sDate%" %%a in ("%1") do (
   for /F "tokens=1-3" %%x in ("!dateOrder[%iDate%]!") do (
      set %%x=%%a& set %%y=%%b& set %%z=%%c
   )
)
if "%DD:~1,1%" equ "" set DD=0%DD%
if "%MM:~1,1%" equ "" set MM=0%MM%
if "%YYYY:~2,2%" equ "" (
   if %YYYY% gtr 80 (
      set YYYY=19%YYYY%
   ) else (
      set YYYY=20%YYYY%
   )
)

:ShowResult

echo Timestamp: %YYYY%%sDate%%MM%%sDate%%DD% @ %H%%sTime%%M%%sTime%%S%

For example:

Code: Select all

C:\Documents and Settings\Antonio\My Documents\ASMB\Batch File\tests
>echo %date% %time% & GetDateTime.bat
12/05/2013 20:58:24.42
Timestamp: 2013/05/12 @ 20:58:24

C:\Documents and Settings\Antonio\My Documents\ASMB\Batch File\tests
>for %a in (*.txt) do @echo %~Ta %a & call GetDateTime %~Ta
12/05/2013 07:57 p.m. GetDateTime.txt
Timestamp: 2013/05/12 @ 19:57:00
03/07/2012 02:08 p.m. TestValueInRanges.txt
Timestamp: 2012/07/03 @ 14:08:00


Antonio

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: How get data/time independent from localization

#39 Post by carlsomo » 12 May 2013 21:34

I wrote this routine in pure batch to get all the date elements for either today's date or any date passed to the routine.
It has an option to prompt a user for a date and does date validation checking as well as accepting or giving julian dates.
It accepts all english and 7 digit julian date formats and whatever the current English format is in use:

Set_Date today

will populate the variables below and more:
today_mdy 05-12-2013
today_ymd 20130512
today_jul 2456885
today_dte Whatever the active regional format, ie. Sun 05/12/2013 or 12-May-13
today_yy 13
today_mm 05
today_yyyy 2013
today_dow Sunday
today_mon May
etc. see the help section at the top:

Code: Select all

@echo off&goto :start
:Set_Date DateEnv [Date] or [/g="Prompt for Date: "] [/r]
echo.
echo.Usage: Set_Date.cmd DateEnv [Date] or [/g="Prompt for Date: "]
echo.Populates date variables labeled: 'DateEnv_???', see below
echo.Accepted date formats:
echo.        m-d-y, m/d/y, yyyymmdd, dd-Mon-yy or 7 digit julian date
echo.        If %%date%% is passed as the 2nd arg it will be interpreted
echo.        according to the current regional date setting ie. yyyy.mm.dd
echo.        If /r is passed then the date contained in the DateEnv variable
echo.        and/or the [Date] argument will be interpreted in regional format
echo.        The /r option should only be used if date input is not included
echo.        in one of the default listed formats or unexpected consequences...
echo.
echo.Valid dates must be in the year 1601 or greater for the validity check
echo.Returns 0 in errorlevel if the date is valid, else non-zero
echo.
echo.If DateEnv is valid or if [Date] is passed DateEnv_??? will be set to:
echo.DateEnv_mdy will be set to mm-dd-yyyy
echo.DateEnv_ymd will be set to yyyymmdd
echo.DateEnv_y.m.d will be set to yyyy.mm.dd
echo.DateEnv_dte will be set to the current active regional date format
echo.DateEnv_jul will be set to the 7 digit Julian date
echo.DateEnv_dow will be set to day of week, ie. Monday, Tuesday, etc.
echo.DateEnv_vrb will be set to Verbose Date, ie. Saturday, August 20th, 2011
echo.DateEnv_dd will be set to the 2 digit day
echo.DateEnv_mm will be set to the 2 digit month
echo.DateEnv_yyyy will be set to the 4 digit year
echo.DateEnv_month will be set to the month, ie. February or December
echo.DateEnv_mon will be set to the 3 letter month, ie. Jan or Aug
echo.
echo.DateEnv itself will not be changed regardless if valid date or not
echo.Also formats yyyymmdd or 7 digit julian date input to desired output
echo.Example: Set_Date today %%date%% or Set_Date newyear99 01-Jan-99
echo.Finally:
echo.If DateEnv is not defined and there is no second argument then %%date%% in
echo.the current regional format will be used to populate the variables
echo.If /g is passed as 2nd argument then quoted 3rd argument will prompt for date
echo.Examples:
echo.         set "AprilsFools=4/1/1"
echo.         set_date AprilFools
echo.         echo Aprils fools date for 2001 in Julian format is: %%AprilFools_jul%%
echo.         echo Aprils fools date in 2001 was on a %AprilFools_dow% ^(Sunday^)
echo.         Set_Date birthday /g="Enter your birth date: "
echo.         ECHO You were born on a %%birthday_dow%%
echo.         set "today="
echo.         Set_Date today
echo.         echo today is %today_dte% or %today_jul% in Julian format
exit /b 1

:start
set "jul="&set "Format="&set "Sep="&set "reg="
if "%~1"=="" exit /b 255
if /i "%~1" equ "/?" goto :Set_Date Syntax
SetLocal EnableDelayedExpansion
echo %*|find /i "/r">nul
if %errorlevel% equ 0 set/a reg=1&if /i "%~1" equ "/r" shift
if defined %~1 (call set jul=%%%~1%%) else (
  if "%~2"=="" set "jul=%date%"
)
if not "%~2"=="" (
  if /i "%~2" neq "/r" (
    set "jul=%~2"&set "args=%~3"
  )
) else (if not defined %~1 set/a reg=1)
call :RegDateFmt Format Separator yy mm dd jul
if /i "%~2" equ "%date%" set/a reg=1
if defined reg (
  set "jul=!mm!-!dd!-!yy!"
)
if /i "%jul%" equ "/g" (
  set/p jul="%args%"
) else if /i "%jul:~0,1%" gtr "9" (
  if defined args set "jul=%jul% %args%"
  set "jul=!jul:* =!"
) else if /i "%jul:~3,1%" gtr "9" (
  set "Mon=%jul:~3,3%"
  call :month_convert Mon
  if !errorlevel! gtr 0 goto :BadDate
  set "jul=!Mon!-%jul:~0,2%-%jul:~-2%"
)
set mdy=%jul:/=%
set mdy=%mdy:-=%
if /i %mdy% equ %jul% (
  call :strlen %mdy%
  if /i !errorlevel! equ 7 (
    call :date_cvt mdy /j
  ) else (call :date_cvt jul /j)
) else (call :date_cvt jul /j)
if /i %errorlevel% equ 0 (
  set/a mdy=%jul%
  set/a dow=%jul% %% 7&call :set_day dow
) else (goto :BadDate)
call :date_cvt mdy /j
set "vrb=%mdy%"
call :format_verbose vrb dow month
set/a ymd=%errorlevel%
set "mon=%month:~0,3%"
set/a dte=%ymd%
call :setRegDate dte Format Separator dow mon
if /i %errorlevel% gtr 0 goto :BadDate
Endlocal&(
  call set "%~1_mdy=%mdy%"&call set "%~1_ymd=%ymd%"&call set "%~1_jul=%jul%"
  call set "%~1_vrb=%vrb%"&call set "%~1_dow=%dow%"&call set "%~1_dd=%ymd:~-2%"
  call set "%~1_mm=%mdy:~0,2%"&call set "%~1_yyyy=%ymd:~0,4%"
  call set "%~1_mon=%mon%"&call set "%~1_yy=%ymd:~2,2%"&call set "%~1_dte=%dte%"
  call set "%~1_y.m.d=%ymd:~0,4%.%mdy:~0,2%.%ymd:~-2%"&call set "%~1_month=%month%"
  exit /b 0
)
:BadDate
Endlocal&(
  call set %~1_mdy=&call set %~1_ymd=&call set %~1_dte=&call set "%~1_jul="
  call set %~1_vrb=&call set %~1_dow=&call set %~1_vrb=&call set "%~1_y.m.d="
  call set "%~1_month="
)&exit /b %errorlevel%

::**********************************::
::*******||  SUBROUTINES   ||*******::
::**********************************::

:set_day
SetLocal&call set/a tkn=%%%~1%%+1
for /f "tokens=%tkn%" %%a in ("Monday Tuesday Wednesday Thursday Friday Saturday Sunday") do (
  set "dayofwk=%%a"
)
EndLocal&call set %~1=%dayofwk%&exit /b 1

:Date_Cvt DateEnv [/Julian] Date_env is converted [only yyyymmdd, or mm-dd-yyyy or julian needed here]
if "%~1"=="" exit /b 1
SetLocal&call set "mdy=%%%~1%%"
set ech=&set "arg="
if not "%~2"=="" (set arg=%~2&set arg=!arg:~0,2!)
xcopy /d:%mdy% /h /l "%~f0" "%~f0\">nul 2>&1
if /i %errorlevel% equ 0 (
  for /f "tokens=1-3 delims=/- " %%a in ("%mdy%") do (
    set m=%%a&set d=%%b&set "y=%%c"
    if /i 1!m! lss 20 set "m=0!m!"
    if /i 1!d! lss 20 set "d=0!d!"
    if /i 1!y! lss 20 set "y=0!y!"
    if /i !y! lss 80 (set y=20!y!) else (if !y! lss 100 set y=19!y!)
    set "mdy=!m!-!d!-!y!"
  )
) else (
  set /a rc=1
  for /f "tokens=1-3 delims=0123456789" %%a in ("%mdy%") do set "rc=%%a"
  if /i !rc! neq 1 set /a err=2&goto :end
  call :strlen %mdy%
  if /i !errorlevel! gtr 8 set /a err=3&goto :end
)
set "mdy=%mdy:/=-%"
set "ymd=%mdy:-=%"
if %ymd%==%mdy% (
  call :strlen %ymd%
  set /a err=!errorlevel!
  if /i !err! equ 7 if /i "%arg%" equ "/j" (
    call :gdate %ymd%
    set /a ymd=!errorlevel!
    set /a err=8&set "arg="
  )
  if /i !err! neq 8 goto :end
  set mdy=!ymd:~4,2!-!ymd:~6,2!-!ymd:~0,4!&set "ech=!mdy!"
) else (
  set ymd=%ymd:~4,4%%ymd:~0,4%&set "ech=!ymd!"
)
xcopy /d:%mdy% /h /l "%~f0" "%~f0\">nul 2>&1
set /a err=%errorlevel%
if /i %err% neq 0 (set ech=)
if /i %err% equ 0 if /i "%arg%" equ "/j" (
  call :jdate %ymd%
  set /a ech=!errorlevel!
)
:end
EndLocal&call set "%~1=%ech%"&exit /b %err%

:Strlen Returns length of string in errorlevel [Simple version for short strings]
setlocal&set "#=%*"
if not defined # exit /b 0
set/a len=0
:loop
set/a len+=1
set "#=!#:~1!"&if not defined # endlocal&exit/b %len%
goto :loop

:jdate
SetLocal
set "yyyymmdd=%~1"
set "yyyy=%yyyymmdd:~0,4%"
set "mm=%yyyymmdd:~4,2%"
set "dd=%yyyymmdd:~6,2%"
if %mm:~0,1% equ 0 set "mm=%mm:~1%"
if %dd:~0,1% equ 0 set "dd=%dd:~1%"
set /a Month1=(%mm%-14)/12
set /a Year1=%yyyy%+4800
set /a JDate=1461*(%Year1%+%Month1%)/4+367*(%mm%-2-12*%Month1%)^
             /12-(3*((%Year1%+%Month1%+100)/100))/4+%dd%-32075
EndLocal&exit /b %JDate%

:gdate
SetLocal
set /a p      = %1 + 68569
set /a q      = 4 * %p% / 146097
set /a r      = %p% - ( 146097 * %q% +3 ) / 4
set /a s      = 4000 * ( %r% + 1 ) / 1461001
set /a t      = %r% - 1461 * %s% / 4 + 31
set /a u      = 80 * %t% / 2447
set /a v      = %u% / 11
set /a GYear  = 100 * ( %q% - 49 ) + %s% + %v%
set /a GMonth = %u% + 2 - 12 * %v%
set /a GDay   = %t% - 2447 * %u% / 80
if /i 1%GMonth% lss 20 set "GMonth=0%GMonth%"
if /i 1%GDay%   lss 20 set "GDay=0%GDay%"
set "GDate=%GYear%%GMonth%%GDay%"
EndLocal&exit /b %GDate%

:Format_Verbose M/D/Y dayofweek to verbose date
SetLocal&call set "dte=%%%~1%%"
set "dow=%%%~2%%"
set "st="
set "day=%dte:~3,2%"
set "mon=%dte:~0,2%"
set "year=%dte:~6,4%"
set "ymd=%year%%mon%%day%"
set "dy=%day:~1%"
if %day:~0,1% equ 0 set "day=%day:~1%"
if %mon:~0,1% equ 0 set "mon=%mon:~1%"
set months=January February March April May June^
 July August September October November December
for /f "tokens=%mon%" %%a in ("%months%") do set "month=%%a"
if /i %dy% equ 0 set "st=th"
if /i %dy% gtr 3 set "st=th"
if /i %day% geq 11 if /i %day% leq 13 set "st=th"
if defined st goto :end
set/a rst=%day% %% 10
for /f "tokens=%rst%" %%s in ("st nd rd") do set "st=%%s"
:end
set "dow=%dow%, %month% %day%%st%, %year%"
EndLocal&call set %~1=%dow%&call set %~3=%month%&exit /b %ymd%

:RegDateFmt Format Separator y m d dte
if "%~2"=="" exit/b 1
setlocal EnabledelayedExpansion
set "Day="&set "Mon="&set "dte="
if not "%~6"=="" set "dte=!%~6!"
if not defined dte set "dte=%date%"
for /f "tokens=2 delims=:" %%A in ('date^<nul') do set Format=%%A&goto :next
:next
set "Format=%Format: =%"
for %%A in (/ - . ,) do (
  echo %Format%|find "%%A">nul&if !errorlevel! equ 0 set "Separator=%%A"
)
if /i %Format:~0,1% gtr 9 set "Day=Day "
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('date^<nul') do (
  set "Format=%Day%%%a%Separator%%%b%Separator%%%c"
  for /f "tokens=1-3 delims=.-/ " %%d in ("%dte:* =%") do (
    set %%a=%%d&set %%b=%%e&set "%%c=%%f"
    echo !yy:~3!|find "ECHO">nul
    if /i "!errorlevel!" neq "0" set "Format=!Format:yy=yyyy!"
    if /i "!mm:~0,1!" gtr "9" (
      set "Format=!Format:mm=Mon!"
      call :month_convert mm
      if /i "!errorlevel!" neq "0" endlocal&exit/b 1
    )
  )
)
endlocal&(
  call set "%~1=%Format%"
  call set "%~2=%Separator%"
  if not "%~3"=="" call set "%~3=%yy%"
  if not "%~4"=="" call set "%~4=%mm%"
  if not "%~5"=="" call set "%~5=%dd%"
)&exit/b 0

:month_convert
set "months=Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
call set "month=%%%~1%%"
set/a mnum=0
for %%m in (%months%) do set/a mnum+=1&if /i %month% equ %%m call set "%~1=!mnum!"&exit /b 0
exit/b 1

:setRegDate dte format sep dow mon
setlocal EnableDelayedExpansion
if /i "%~5"=="" exit/b 1
set mon=!%~5!&set dow=!%~4!&set "dow=!dow:~0,3! "
set sep=!%~3!&set "format=!%~2!"&set "dte=!%~1!"
set yyyy=%dte:~0,4%&set yy=%dte:~2,2%&set mm=%dte:~4,2%&set "dd=%dte:~-2%"
set "toks=2-4"
echo %format%|find " ">nul
if %errorlevel% equ 1 set "dow="& set "toks=1-3"
for /f "tokens=%toks% delims=%sep% " %%a in ("%format%") do (
  set "dte=%dow%!%%a!%sep%!%%b!%sep%!%%c!"
)
endlocal&call set "%~1=%dte%"&exit/b 0


Copy paste and try it out. I know it works on XP, Vista, Win7
I would appreciate feedback from anyone who tries it out.

Carl
Last edited by carlsomo on 13 May 2013 19:16, edited 1 time in total.

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

Re: How get data/time independent from localization

#40 Post by foxidrive » 13 May 2013 02:36

It works here in Win 8 Carl. However it does nothing if I launch it by name though - no variables and no help.

Code: Select all

today_dd=13
today_dow=Monday
today_dte=Mon 13/05/2013
today_jul=2456426
today_mdy=05-13-2013
today_mm=05
today_mon=May
today_month=May
today_vrb=Monday, May 13th, 2013
today_y.m.d=2013.05.13
today_ymd=20130513
today_yy=13
today_yyyy=2013

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

Re: How get data/time independent from localization

#41 Post by carlos » 13 May 2013 05:45

I thinks is not good try rebuild the information from the datetime format configuration from register, because if I not bad remember, there are a 2 configs in HKCU\Control Panel\International that only show the year with two digits, in two formats:
[dd/MM/yy]
[yy.MM.dd]
then if you try parse and separate the information considering all other configs, how you distinguish if you have a 01 and 10, if 01 refers to january or 2001, or to october and 2010 ?
Also, because the large amount of configurations is easy insert a bug in the code. Why not use the method using makecab, is faster, and the information come in a unique format?

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

Re: How get data/time independent from localization

#42 Post by Aacini » 13 May 2013 08:35

@carlos:

iDate regional setting gives that information. Didn't you reviewed my solution?
Aacini wrote:HKCU\Control Panel\International subkey - Regional and Language Options
http://technet.microsoft.com/en-us/libr ... 42(v=ws.10).aspx

Name Description Standard value

iDate date order 0=mm/dd/yy, 1=dd/mm/yy, 2=yy/mm/dd (short date)


set dateOrder[0]=MM DD YYYY
set dateOrder[1]=DD MM YYYY
set dateOrder[2]=YYYY MM DD

On the other hand, I don't see how the makecab method could be used to get the timestamp of the created date of a file. Let's remember the original request:
darioit wrote:Hello,

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

Best Regards


Antonio

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: How get data/time independent from localization

#43 Post by carlsomo » 13 May 2013 19:04

foxidrive wrote:It works here in Win 8 Carl. However it does nothing if I launch it by name though - no variables and no help.

Code: Select all

today_dd=13
today_dow=Monday
today_dte=Mon 13/05/2013
today_jul=2456426
today_mdy=05-13-2013
today_mm=05
today_mon=May
today_month=May
today_vrb=Monday, May 13th, 2013
today_y.m.d=2013.05.13
today_ymd=20130513
today_yy=13
today_yyyy=2013


Sorry, please type Set_Date /? at the command line to get all the help displayed.
You can go into control panel to reset your regional date setting to anything in english
and get proper results. I haven't tested beyond XP, Vista and Win 7.

You can also use it like this:
Set_date A_date 29-Jan-13
to populate all the A_Date_??? variables with January 29, 2013.

Or to get a date from the user:
Set_Date Birthday /g="Please enter your birth date [m-d-y]: "
If the user enters: 1/1/1 you will get 01-01-2001 into the 'Birthday_mdy' variable, etc.
I have found it to be an incredibly useful routine for getting date variables when my routines
are run on other machines where you don't know for sure what date format they are using.
Since it is an 'all batch' routine you don't need to worry about user privileges or get into the registry.
I used to have to set the date format in all the machines, now I don't worry about it.

Carl

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: How get data/time independent from localization

#44 Post by npocmaka_ » 08 Nov 2013 02:51

As I like the robocopy approach - I think this is the fastest solution with it:

Code: Select all

@echo off
setlocal
for /f "skip=10 tokens=2,3,4,5,6,7,8 delims=: " %%D in ('robocopy /l * \ \ /ns /nc /ndl /nfl /np /njh /XF * /XD *') do (
 set "dow=%%D"
 set "month=%%E"
 set "day=%%F"
 set "HH=%%G"
 set "MM=%%H"
 set "SS=%%I"
 set "year=%%J"
)
 
echo Day of the week: %dow%
echo Day of the month : %day%
echo Month : %month%
echo hour : %HH%
echo minutes : %MM%
echo seconds : %SS%
echo year : %year%
endlocal
Last edited by npocmaka_ on 08 Nov 2013 07:49, edited 1 time in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How get data/time independent from localization

#45 Post by dbenham » 08 Nov 2013 06:37

@npocmaka - As posted, your code failed. I get:

Code: Select all

Day of the week:  Fri Nov 08 07
Day of the month : 08 2013
Month : 30
hour :
minutes :
seconds :
year :

Presumably, your DELIMS option was set to <colon><tab><space>, but the tab was converted to 3 spaces when posted. Having multiple spaces at the end causes the <space> to no longer be recognized as a delimiter :!: There must be exactly one space at the end.

I don't see any tabs in the output, so I got the code to "work" by using DELIMS=<colon><space>

However, I should think most scenarios require a numeric value for the month, (and possibly for the day of week). I suppose a simple translation table could be used. But does ROBOCOPY always use English for the day of week and month abbreviations? If not, then I don't see how this could be considered locale independent.


Dave Benham

Post Reply