Retrieving Date/Time/Number format from Windows

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
zebuddha
Posts: 12
Joined: 03 Nov 2015, 10:17

Retrieving Date/Time/Number format from Windows

15 Jun 2016, 02:58

Hello there,
I'm wondering how I can retrieve the current windows settings for the date/number formatting.

What I need this for is to be able to input existing, given dates/numbers (from an excel sheet) into a tool that requires a specific date/number format.
If I use COM objects to retrieve the dates/numbers from Excel, they are retrieved according to system locale/format settings, so I need to know what they are so that my script can auto-convert them to the correct format.

Until now I've used a rather crude string split, etc. but it can easily cause severe errors as it's not capable of differentiating US dates (MM-DD) from EU dates (DD-MM), or dates where the year is in a 2 char format (e.g. DD-MM-YY vs YY-MM-DD).

Similarly, for numbers it causes issues if the decimal and/or thousands separators vary (e.g. 1,234.789 vs 1.234,789). In this case, if the numbers were always formatted exactly the same (e.g. 3 decimal places) then it might not be an issue, but as it's not the case (anything from 0 to 6 decimal places, with numbers anywhere between 100 000 000 and 0.000001) it's rather hard to have this fool-proof.

Anyhow, thanks in advance for any ideas on how to solve this.
User avatar
ahkDustVorteX
Posts: 47
Joined: 14 May 2014, 12:08

Re: Retrieving Date/Time/Number format from Windows

15 Jun 2016, 06:41

There is a variable in AHK that you can retrieve the full Date string: A_Now
Then you can format as you want with FormatTime command.
Please see the documentation.

Regards
Editor: Notepad++
"Those who wait and persist, always achieve."
zebuddha
Posts: 12
Joined: 03 Nov 2015, 10:17

Re: Retrieving Date/Time/Number format from Windows

16 Jun 2016, 02:30

Hi Dust,
Thanks, but that's not really what I'm looking for. What I'm trying to do is the reverse, retrieve the EXISTING date/number format from Windows, and then use that to know what I need to do to convert a series of existing dates/numbers (already formatted - incorrectly) into the format I want/need.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Retrieving Date/Time/Number format from Windows

17 Jun 2016, 01:26

For GetNumberFormat & GetNumberFormatEx I did the translation already => WinAPI - GetXxxFormat Functions
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
just me
Posts: 9482
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Retrieving Date/Time/Number format from Windows

17 Jun 2016, 03:14

Thought I saw a similar posting by querty12 here, but if so it obviously disappeared.

This might give you some values of interest:

Code: Select all

; ==================================================================================================================================
; GetLocaleInfo()       - Retrieves information about a locale specified by identifier.
; Parameters:
;     LCType            - one of the key names defined class LOCALEINFO
;     LCID (optional)   - valid LCID (-> msdn.microsoft.com/en-us/library/dd464799(VS.85).aspx)
;                         Default: 0x0400 (LOCALE_USER_DEFAULT)
; Return values:
;     Queried value or empty string in case of errors.
; MSDN:
;     msdn.microsoft.com/en-us/library/dd318101(v=vs.85).aspx
; ==================================================================================================================================
GetLocaleInfo(LCType, LCID := 0x0400) {
   Static LOCALE_RETURN_NUMBER := 0x20000000   ; return number instead of string
        , SizeT := A_IsUnicode ? 2 : 1
   If !(Item := LOCALEINFO[LCType])
      Return ""
   If (SubStr(LCType, 1, 1) = "I") {
      DllCall("GetLocaleInfo", "UInt", LCID, "UInt", Item | LOCALE_RETURN_NUMBER, "UIntP", N, "Int", 4 // SizeT)
      Return N
   }
   L := DllCall("GetLocaleInfo", "UInt", LCID, "UInt", Item, "Str", "", "Int", 0)
   VarSetCapacity(S, L * SizeT, 0)
   DllCall("GetLocaleInfo", "UInt", LCID, "UInt", Item, "Str", S, "Int", L)
   Return S
}
; ==================================================================================================================================
; Class LOCALEINFO - auxiliary class to define the "LOCALE_..." constants.
; Only constants supported by Win XP are included.
; MSDN -> msdn.microsoft.com/en-us/library/dd464799(VS.85).aspx
; ==================================================================================================================================
Class LOCALEINFO {
   ; Number formats --------------------------
   Static ICALENDARTYPE          := 0x00001009   ; type of calendar specifier, eg CAL_GREGORIAN
   Static ICENTURY               := 0x00000024   ; century format specifier (short date, SSHORTDATE is preferred)
   Static ICOUNTRY               := 0x00000005   ; country code, eg 1, SISO3166CTRYNAME may be more useful.
   Static ICURRDIGITS            := 0x00000019   ; # local monetary digits, eg 2 for $1.00
   Static ICURRENCY              := 0x0000001B   ; positive currency mode, 0-3, see documenation
   Static IDATE                  := 0x00000021   ; short date format ordering (derived from SSHORTDATE, use that instead)
   Static IDAYLZERO              := 0x00000026   ; leading zeros in day field (short date, SSHORTDATE is preferred)
   Static IDEFAULTANSICODEPAGE   := 0x00001004   ; default ansi code page (use of Unicode is recommended instead)
   Static IDEFAULTCODEPAGE       := 0x0000000B   ; default oem code page (use of Unicode is recommended instead)
   Static IDEFAULTCOUNTRY        := 0x0000000A   ; default country code, deprecated
   Static IDEFAULTEBCDICCODEPAGE := 0x00001012   ; default ebcdic code page (use of Unicode is recommended instead)
   Static IDEFAULTLANGUAGE       := 0x00000009   ; default language id, deprecated
   Static IDEFAULTMACCODEPAGE    := 0x00001011   ; default mac code page (use of Unicode is recommended instead)
   Static IDIGITS                := 0x00000011   ; number of fractional digits eg 2 for 1.00
   Static IDIGITSUBSTITUTION     := 0x00001014   ; 0 = context, 1 = none, 2 = national
   Static IFIRSTDAYOFWEEK        := 0x0000100C   ; first day of week specifier, 0-6, 0=Monday, 6=Sunday
   Static IFIRSTWEEKOFYEAR       := 0x0000100D   ; first week of year specifier, 0-2, see documentation
   Static IGEOID                 := 0x0000005B   ; geographical location id, eg "244"
   Static IINTLCURRDIGITS        := 0x0000001A   ; # intl monetary digits, eg 2 for $1.00
   Static ILANGUAGE              := 0x00000001   ; language id, SNAME preferred
   Static ILDATE                 := 0x00000022   ; long date format ordering (derived from SLONGDATE, use that instead)
   Static ILZERO                 := 0x00000012   ; leading zeros for decimal, 0 for .97, 1 for 0.97
   Static IMEASURE               := 0x0000000D   ; 0 = metric, 1 = US measurement system
   Static IMONLZERO              := 0x00000027   ; leading zeros in month field (short date, SSHORTDATE is preferred)
   Static INEGCURR               := 0x0000001C   ; negative currency mode, 0-15, see documentation
   Static INEGNUMBER             := 0x00001010   ; negative number mode, 0-4, see documentation
   Static INEGSEPBYSPACE         := 0x00000057   ; mon sym sep by space from neg amt (derived from INEGCURR)
   Static INEGSIGNPOSN           := 0x00000053   ; negative sign position (derived from INEGCURR)
   Static INEGSYMPRECEDES        := 0x00000056   ; mon sym precedes neg amt (derived from INEGCURR)
   Static IOPTIONALCALENDAR      := 0x0000100B   ; additional calendar types specifier, eg CAL_GREGORIAN_US
   Static IPAPERSIZE             := 0x0000100A   ; 1 = letter, 5 = legal, 8 = a3, 9 = a4
   Static IPOSSEPBYSPACE         := 0x00000055   ; mon sym sep by space from pos amt (derived from ICURRENCY)
   Static IPOSSIGNPOSN           := 0x00000052   ; positive sign position (derived from INEGCURR)
   Static IPOSSYMPRECEDES        := 0x00000054   ; mon sym precedes pos amt (derived from ICURRENCY)
   Static ITIME                  := 0x00000023   ; time format specifier (derived from STIMEFORMAT, use that instead)
   Static ITIMEMARKPOSN          := 0x00001005   ; time marker position (derived from STIMEFORMAT, use that instead)
   Static ITLZERO                := 0x00000025   ; leading zeros in time field (derived from STIMEFORMAT, use that instead)
   ; String formats --------------------------
   Static S1159                  := 0x00000028   ; AM designator, eg "AM"
   Static S2359                  := 0x00000029   ; PM designator, eg "PM"
   Static SABBREVCTRYNAME        := 0x00000007   ; arbitrary abbreviated country name, SISO3166CTRYNAME preferred
   Static SABBREVDAYNAME1        := 0x00000031   ; abbreviated name for Monday
   Static SABBREVDAYNAME2        := 0x00000032   ; abbreviated name for Tuesday
   Static SABBREVDAYNAME3        := 0x00000033   ; abbreviated name for Wednesday
   Static SABBREVDAYNAME4        := 0x00000034   ; abbreviated name for Thursday
   Static SABBREVDAYNAME5        := 0x00000035   ; abbreviated name for Friday
   Static SABBREVDAYNAME6        := 0x00000036   ; abbreviated name for Saturday
   Static SABBREVDAYNAME7        := 0x00000037   ; abbreviated name for Sunday
   Static SABBREVLANGNAME        := 0x00000003   ; arbitrary abbreviated language name, SISO639LANGNAME preferred
   Static SABBREVMONTHNAME1      := 0x00000044   ; abbreviated name for January
   Static SABBREVMONTHNAME2      := 0x00000045   ; abbreviated name for February
   Static SABBREVMONTHNAME3      := 0x00000046   ; abbreviated name for March
   Static SABBREVMONTHNAME4      := 0x00000047   ; abbreviated name for April
   Static SABBREVMONTHNAME5      := 0x00000048   ; abbreviated name for May
   Static SABBREVMONTHNAME6      := 0x00000049   ; abbreviated name for June
   Static SABBREVMONTHNAME7      := 0x0000004A   ; abbreviated name for July
   Static SABBREVMONTHNAME8      := 0x0000004B   ; abbreviated name for August
   Static SABBREVMONTHNAME9      := 0x0000004C   ; abbreviated name for September
   Static SABBREVMONTHNAME10     := 0x0000004D   ; abbreviated name for October
   Static SABBREVMONTHNAME11     := 0x0000004E   ; abbreviated name for November
   Static SABBREVMONTHNAME12     := 0x0000004F   ; abbreviated name for December
   Static SABBREVMONTHNAME13     := 0x0000100F   ; abbreviated name for 13th month (if exists)
   Static SCOUNTRY               := 0x00000006   ; localized name of country, eg "Germany" in UI language
   Static SCURRENCY              := 0x00000014   ; local monetary symbol, eg "$"
   Static SDATE                  := 0x0000001D   ; date separator (derived from SSHORTDATE, use that instead)
   Static SDAYNAME1              := 0x0000002A   ; long name for Monday
   Static SDAYNAME2              := 0x0000002B   ; long name for Tuesday
   Static SDAYNAME3              := 0x0000002C   ; long name for Wednesday
   Static SDAYNAME4              := 0x0000002D   ; long name for Thursday
   Static SDAYNAME5              := 0x0000002E   ; long name for Friday
   Static SDAYNAME6              := 0x0000002F   ; long name for Saturday
   Static SDAYNAME7              := 0x00000030   ; long name for Sunday
   Static SDECIMAL               := 0x0000000E   ; decimal separator, eg "." for 1,234.00
   Static SENGCOUNTRY            := 0x00001002   ; English name of country, eg "Germany"
   Static SENGCURRNAME           := 0x00001007   ; english name of currency, eg "Euro"
   Static SENGLANGUAGE           := 0x00001001   ; English name of language, eg "German"
   Static SENGLISHCOUNTRYNAME    := 0x00001002   ; English name of country, eg "Germany"
   Static SENGLISHLANGUAGENAME   := 0x00001001   ; English name of language, eg "German"
   Static SGROUPING              := 0x00000010   ; digit grouping, eg "3;0" for 1,000,000
   Static SINTLSYMBOL            := 0x00000015   ; intl monetary symbol, eg "USD"
   Static SISO3166CTRYNAME       := 0x0000005A   ; ISO abbreviated country name, eg "US"
   Static SISO639LANGNAME        := 0x00000059   ; ISO abbreviated language name, eg "en"
   Static SLANGUAGE              := 0x00000002   ; localized name of locale, eg "German (Germany)" in UI language
   Static SLIST                  := 0x0000000C   ; list item separator, eg "," for "1,2,3,4"
   Static SLOCALIZEDCOUNTRYNAME  := 0x00000006   ; localized name of country, eg "Germany" in UI language
   Static SLOCALIZEDDISPLAYNAME  := 0x00000002   ; localized name of locale, eg "German (Germany)" in UI language
   Static SLONGDATE              := 0x00000020   ; long date format string, eg "dddd, MMMM dd, yyyy"
   Static SMONDECIMALSEP         := 0x00000016   ; monetary decimal separator, eg "." for $1,234.00
   Static SMONGROUPING           := 0x00000018   ; monetary grouping, eg "3;0" for $1,000,000.00
   Static SMONTHNAME1            := 0x00000038   ; long name for January
   Static SMONTHNAME2            := 0x00000039   ; long name for February
   Static SMONTHNAME3            := 0x0000003A   ; long name for March
   Static SMONTHNAME4            := 0x0000003B   ; long name for April
   Static SMONTHNAME5            := 0x0000003C   ; long name for May
   Static SMONTHNAME6            := 0x0000003D   ; long name for June
   Static SMONTHNAME7            := 0x0000003E   ; long name for July
   Static SMONTHNAME8            := 0x0000003F   ; long name for August
   Static SMONTHNAME9            := 0x00000040   ; long name for September
   Static SMONTHNAME10           := 0x00000041   ; long name for October
   Static SMONTHNAME11           := 0x00000042   ; long name for November
   Static SMONTHNAME12           := 0x00000043   ; long name for December
   Static SMONTHNAME13           := 0x0000100E   ; long name for 13th month (if exists)
   Static SMONTHOUSANDSEP        := 0x00000017   ; monetary thousand separator, eg "," for $1,234.00
   Static SNATIVECOUNTRYNAME     := 0x00000008   ; native name of country, eg "Deutschland"
   Static SNATIVECTRYNAME        := 0x00000008   ; native name of country, eg "Deutschland"
   Static SNATIVECURRNAME        := 0x00001008   ; native name of currency, eg "euro"
   Static SNATIVEDIGITS          := 0x00000013   ; native digits for 0-9, eg "0123456789"
   Static SNATIVELANGNAME        := 0x00000004   ; native name of language, eg "Deutsch"
   Static SNATIVELANGUAGENAME    := 0x00000004   ; native name of language, eg "Deutsch"
   Static SNEGATIVESIGN          := 0x00000051   ; negative sign, eg "-"
   Static SPOSITIVESIGN          := 0x00000050   ; positive sign, eg ""
   Static SSHORTDATE             := 0x0000001F   ; short date format string, eg "MM/dd/yyyy"
   Static SSORTNAME              := 0x00001013   ; sort name, usually "", eg "Dictionary" in UI Language
   Static STHOUSAND              := 0x0000000F   ; thousand separator, eg "," for 1,234.00
   Static STIME                  := 0x0000001E   ; time separator (derived from STIMEFORMAT, use that instead)
   Static STIMEFORMAT            := 0x00001003   ; time format string, eg "HH:mm:ss"
   Static SYEARMONTH             := 0x00001006   ; year month format string, eg "MM/yyyy"
}
Usage sample:

Code: Select all

#NoEnv
#Include GetLocaleInfo.ahk
LCID := DllCall("GetThreadLocale", "UInt")
MsgBox, 0, SSHORTDATE, % GetLocaleInfo("SSHORTDATE")
MsgBox, 0, ILANGUAGE, % GetLocaleInfo("ILANGUAGE", LCID)
MsgBox, 0, SNATIVECOUNTRYNAME, % GetLocaleInfo("SNATIVECOUNTRYNAME", 0x0C0A) ; 0x0C0A -> Spain
ExitApp
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Retrieving Date/Time/Number format from Windows

17 Jun 2016, 05:40

just me wrote:Thought I saw a similar posting by querty12 here, but if so it obviously disappeared.
I was hoping to silently let that go, but, yes, I deleted it myself - I thought it was the wrong approach when, as already mentioned, GetDateFormat etc. exists. I only looked at GetLocaleInfoEx, so what I posted wouldn't run on XP, and your code appears to handle more cases, so your post is better than what I had anyway.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 416 guests