Convert different formats for currency / numbers or date - to the same structure

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Convert different formats for currency / numbers or date - to the same structure

11 Sep 2020, 03:42

1) Currency
Currency / numbers can be presented in a variety of ways.
Is there a way to handle this or does each number have to be handled individually?
(The desire is to be able to add, multiply, divide, subtract, etc. with these numbers)
Some examples for 1000 with two decimals.: as 1000.00 (or as 1000,00 as on my computer)
  • 1.000,00 => 1000.00
  • 1,000.00 => 1000.00
  • 1 000,00 => 1000.00
  • 1000 => 1000.00
    etc.
2) Dates
Dates feel much harder to handle. The variations are many more and the language plays a greater role.
My wish is to be able to calculate dates.
(eg what date is it in 30 days)

Code: Select all

var1 := "20201020"
var1 += 30, days
MsgBox % var1
Some examples for October 5, 2020
  • 2020-10-05 => 20201005
  • 2020/10/05 => 20201005
  • 5-10-2020 => 20201005
  • 19-8-2020 => 20200819
  • 201005 => 20201005
  • 051020 => 20201005
  • 5 okt 2020 => 20201005
  • 05 oct 2020 => 20201005
  • 5 oktober 2020 => 20201005
    etc.
Is there a way to handle this or does each dates have to be handled individually?
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

11 Sep 2020, 05:00

AHK is great for formatting. See Format() and FormatTime.
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: Convert different formats for currency / numbers or date - to the same structure

11 Sep 2020, 08:04

Thank you!

Format() is a way to handle AHK's date format (20201011145441) to
date and time, and this time string can be handled in many ways.
- but vice versa, in different languages ....
I'm not sure Format() is the right solution.
I have no idea how Format() could help me (in the date question)

The currency question, must be handled in a different way.
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

11 Sep 2020, 08:54

You're right. You need a currency "converter". Those scripts above look promising.
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: Convert different formats for currency / numbers or date - to the same structure

16 Sep 2020, 16:37

Thought all my dates could be handled - with the links from @BoBo . (but I was wrong - currency seems to work)
Is there a convert from date format in text, to AHK date format?
eg. Tue Aug 11 13:59:27 2020 to (YYYYMMDDHH24MISS) 20200811135927

Below I have some dates that I want to convert to a format that AHK can count on (add or subtract days).
For example, I want to count .:
1) How many days is it between .: Tue Aug 11 13:59:27 2020 and Mon Aug 24 14:27:52 2020 ?
2) How many days is it between (format .: YYMMDD) .: 200811 and 201010 ?
3) Add days to (format .: YYYY-MM-DD) .: 2020-07-28 + 30 days ?
4) Add days to (format .: DD-M-YYYY) .: 30-7-2020 + 10 days ?
it was some examples.
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

16 Sep 2020, 18:13

Nothing built in, but you could always write your own. It may not be too tough, as I think you would do the following.

1. Identify the string's format. The ones that you provided would be easy to distinguish from each other.

2. Based on #1, convert to AHK timestamp format.

3. Do the math from there-- easy with EnvAdd, for example.
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: Convert different formats for currency / numbers or date - to the same structure

17 Sep 2020, 04:18

I found a function() that tries to convert some specific date formats in the AHK manual FormatTime(). There is a link to .: Date parser - convert any date format to YYYYMMDDHH24MISS by @polyethene (but does not convert the date formats and languages ​​I need to convert)

The above date presentations (in my example) does not follow the same pattern, becoming more difficult to identify when days or months only consist of only one digit (not two). Then there is a language issue that is not entirely simple. (e.g. 9 or 09 or Sep or Sept or September is the same month). In Sweden it is almost the same (but name of the month is only in lowercases)

Presentations of the clock time may also differ. Sometimes the time is presented (as above hh:mm:ss - 13:01:00) or 13:1:0 or pm 1:1:1 or ... (Maybe there is a function that handles the time for AHK.)

Yes, have thought about writing a function() myself.
The problem is how many format the function() can handle. Some format maybe the function dateParser() kan handle?
Must there be a separate function() for each different date format to be converted?
Something like DateConvert("Tue Aug 11 13:59:27 2020", "Type1") and DateConvert("2020/10/05", "Type2") and DateConvert("201010 ", "Type3")
Maybe better to write a function() that can handle more formats - similar to DateConvert ("Tue Aug 11 13:59:27 2020", "ddd MMM dd hh: mm: ss YYYY") (However, a little harder)
The hardest thing (I see now) is to handle the clock - 10:10:01 or 10:10:01 A or AM 10:10:01 (I feel unsure about how AM / PM is presented. - may not encounter it either)

One way to manage the months is to build an object - as well as compare and try to find the right month and convert to numbers.
(it does not matter if the compare is on Swedish or another language) like this .:
(Excerpt from a small test)


Edit .: Forgot to mention that this feature seems to exist in other programming languages (SQL?)
How to convert from a date-time (in varchar format) to dddyyyy format (varchar)?
(to_date('2019 04 19 03:00:00', 'yyyy mm dd hh24:mi:ss') or to_char(to_date('2019 04 19 03:00:00', 'yyyy mm dd hh24:mi:ss'), 'dddyyyy'))
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

17 Sep 2020, 06:58

Parsing into date and time should be easy due to consistent use of ":". Next, for dates, you would have to know the locale, to know the order of mm and dd. After that, the rest of the parsing should be straightforward.
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

17 Sep 2020, 10:02

This works for all of your date examples.

Code: Select all

; Parse the date
; @mikeyww on 17 September 2020
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=80896

locale = Month first ; Change if not true
If (locale != "Month first")
 locale = Day first
dts := ["Oct 5, 2020", "2020-10-05", "2020/10/05", "5-10-2020", "19-8-2020", "201005", "051020", "5 okt 2020", "05 oct 2020", "5 oktober 2020"] ; Dates to test
Global mos := {okt: 10, oct: 10}
For index1, dt in dts {
 MsgBox,, %locale%, % "Original:       " . dt . "`nyyyymmdd:   " . yyyymmdd(dt, locale)
}
ExitApp

yyyymmdd(dateString, locale){
 part := [], new := [], dt := StrReplace(dateString, ","), md := locale = "Month first" ? True : False
 If RegExMatch(dt, "\d{6}") { ; Date string is 6 digits
  Loop, 3 {
   part[A_Index] := SubStr(dt, A_Index*2-1, 2)
   If (A_Index !=2 && part[A_Index] > 12) ; Ensure that year has four digits
    part[A_Index] += 2000
  }
 } Else { ; Date string is not 6 digits
  part := StrSplit(dt, [A_Space, "/", "-"]) ; Get year, month, day
  For index2, this in part
  {
   If this is alpha
   {
    For moName, moNum in mos
    {
     If SubStr(this, 1, 3) = moName ; Month matched a known string
      part[index2] := moNum, md := index2 = 1 ? True : False ; If string starts with alpha month, then day follows it
    }
   }
  }
 }
 For index2, this in part
 {
  If !RegExMatch(this, "\d{4}") { ; This part is not the year
   md := this > 12 ? (index2 > 1 ? True : False) : md ; For numbers > 12, if it's second, then month precedes it
   new[new[3-md] ? md+2 : 3-md] := this ; Populate the month or day slot
  } Else new[1] := this, md := index2 = 1 ? True : md ; If year comes first, then the month follows it
 }
 Return Format("{}{:02}{:02}", new[1], new[2], new[3])
}
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

17 Sep 2020, 12:21

Time issue looks fairly straightforward.

1. Parse the time string into the digits part and the "AM/PM" part.

2. If "P" is present and hour < 12, then add 12 to the hour.

3. Numbers can be simply padded with zeroes if needed.
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

17 Sep 2020, 12:34

With your months of choice, you can simply use the following in the date script.

Code: Select all

Global mos := {jan: 1, feb: 2, mar: 3, apr: 4, maj: 5, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, okt: 10, oct: 10, nov: 11, dec: 12}
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: Convert different formats for currency / numbers or date - to the same structure

17 Sep 2020, 14:16

Thanks for your time!
Have no idea how it works... (a little impressed :? )
(whats differ with locale "Day first" and locale "Month first"?)

Opened a number of files to analyze which date formats I will need to process in the near future
These date formats I did not get to work with your function()
  • vDate := yyyymmdd("Tue Aug 11 13:59:27 2020", "Month first")
  • vDate := yyyymmdd("30 juli 2020", "Day first")
  • vDate := yyyymmdd("09-10-20", "day First") ; dd-mm-yy
  • vDate := yyyymmdd("15-05-20", "Day First") ; dd-mm-yy
  • vDate := yyyymmdd("200820", "day First") ; ddmmyy
  • vDate := yyyymmdd("200730", "Month First") ; yymmdd
  • vDate := yyyymmdd("200818", "Month First") ; yymmdd
  • vDate := yyyymmdd("200928", "Month First") ; yymmdd
  • vDate := yyyymmdd("20201013", "Month First")
  • vDate := yyyymmdd("20-05-06", "Month First") ; yy-mm-dd
  • vDate := yyyymmdd("20.07.03", "Month First") ; yy.mm.dd
  • vDate := yyyymmdd("18.08.2020", "day First")
  • vDate := yyyymmdd("13.10.2020", "Day First")

    Something I can not explain is this .:
    This doesn't work .: Date := "200818" ; yymmdd
    But, this works .: Date := "200730" ; yymmdd

    Some of these may the function DateParse() solve.
    little Test of different date formats
    Edit .: Excuse me - I forgot the Global mos (now added)
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

17 Sep 2020, 15:18

Adjusted here.

Code: Select all

; Parse the date
; @mikeyww on 17 September 2020
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=80896
; Version 02

Global locale := "Month first" ; Change if not true
If (locale != "Month first")
 locale = Day first
dts := ["05 oct 2020"
 , "051020"
 , "09-10-20"
 , "13.10.2020"
 , "15-05-20"
 , "18.08.2020"
 , "19-8-2020"
 , "20.07.03"
 , "20-05-06"
 , "200730"
 , "200818"
 , "200820"
 , "200928"
 , "201005"
 , "2020/10/05"
 , "2020-10-05"
 , "20201013"
 , "30 juli 2020"
 , "5 okt 2020"
 , "5 oktober 2020"
 , "5-10-2020"
 , "Oct 5, 2020"]
Global mos := {jan: 1, feb: 2, mar: 3, apr: 4, maj: 5, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, okt: 10, oct: 10, nov: 11, dec: 12}
For index, dt in dts {
 MsgBox,, % Format("#{} of {} ({})", index, dts.MaxIndex(), locale), % "Original:       " . dt . "`nyyyymmdd:   " . yyyymmdd(dt)
}
ExitApp

yyyymmdd(dateString) {
 yeard := False, part := [], new := [], dt := StrReplace(dateString, ","), md := locale = "Month first" ? True : False
 If RegExMatch(dt, "\d{8}")
  dt := SubStr(dt, 1, 4) . "-" . SubStr(dt, 5, 2) . "-" . SubStr(dt, 7)
 If RegExMatch(dt, "^\d{2}[-.]\d{2}[-.]\d{2}")
  dt := RegExReplace(dt, "[-.]")
 If RegExMatch(dt, "\d{6}") { ; Date string is 6 digits
  Loop, 3 {
   part[A_Index] := SubStr(dt, A_Index*2-1, 2)
   If !yeard && A_Index !=2 && part[A_Index] > 19 ; Ensure that year has four digits
    part[A_Index] += 2000, yeard := True
  }
 } Else { ; Date string is not 6 digits
  part := StrSplit(dt, [A_Space, "/", "-", "."]) ; Get year, month, day
  For index, this in part
  {
   If this is alpha
   {
    For moName, moNum in mos
    {
     If SubStr(this, 1, 3) = moName ; Month matched a known string
      part[index] := moNum, md := index = 1 ? True : False ; If string starts with alpha month, then day follows it
    }
   }
  }
 }
 For index, this in part
 {
  If !RegExMatch(this, "\d{4}") { ; This part is not the year
   md := this > 12 ? (index > 1 ? True : False) : md ; For numbers > 12, if it's second, then month precedes it
   new[new[3-md] ? md+2 : 3-md] := this ; Populate the month or day slot
  } Else new[1] := this, md := index = 1 ? True : md ; If year comes first, then the month follows it
 }
 Return Format("{}{:02}{:02}", new[1], new[2], new[3])
}
The first entry, with year after time, does not make much sense, so I have not included it here. By removing times and days of week from the date strings-- easy to do with AHK-- then even that string format should work.

It's not very elegant, really. There are ways to improve upon this.

Finally, I will add that certain ambiguities remain. For example, is "20-03-21" for 21 March 2020, or 20 March 2021? Some assumptions must be made.
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: Convert different formats for currency / numbers or date - to the same structure

18 Sep 2020, 06:58

Perfect! :dance: (most date formats are now interpreted.)
(Since I do not really understand all the steps in your solution, the questions may feel banal)
Will use this function(), but have some thoughts.

  • 1.) Must the array mos be Global and placed outside the function yyyymmdd()?
  • 1a) I want it inside the function() yyyymmdd()
  • 1b) Why must it be Global?
  • 1c) Will supplement it with additional names of months that may occur (like this)

    Code: Select all

    mos := {	jan: 1, januari: 1, January: 1, feb: 2, febr: 2, februari: 2, February: 2, mar: 3, mars: 3,	March: 3, apr: 4,	april: 4, maj: 5,	may: 5, jun: 6, juni: 6, June: 6, jul: 7,	juli: 7,	July: 7, aug: 8,	augusti: 8,	August: 8, sep: 9,	sept: 9,	september: 9, okt: 10,	oct: 10,	oktober: 10, October: 10, nov: 11,	november: 11, dec: 12,	december: 12	}
    Is it possible to split an array like this instead? (will be easier to read)

    Code: Select all

    mos := {	jan: 1,	januari: 1, January: 1,
    			feb: 2,	febr: 2,	februari: 2, February: 2,
    			mar: 3,	mars: 3,	March: 3,
    			apr: 4,	april: 4,
    			maj: 5,	may: 5,
    			jun: 6,	juni: 6,	June: 6,
    			jul: 7,	juli: 7,	July: 7,
    			aug: 8,	augusti: 8,	August: 8,
    			sep: 9,	sept: 9,	september: 9,
    			okt: 10,	oct: 10,	oktober: 10, October: 10,
    			nov: 11,	november: 11,
    			dec: 12,	december: 12	}



2)
I don't understand the function of the variable locale (Day first / Month first)
I do not know when e.g. Month first must be selected. (I feel unsure which formats the locale handles and which ones are ignored)
Is it only for handling dates with e.g. sex digits .: 100520 (ddmmyy) - Day first? and 051020 (mmddyy) - Month first?

The function can not handle 05102020 (ddmmyyyy) - Day first?

Some other dates locale handles?

The choice of locale does not affect any other date format? (i.e. not 10122020 or...)

3a.)
My wish is to keep everything that belongs to the function yyyymmdd() in the function().

3b.)
As I see it, there are two solutions .:
Now is the function() like this.: yyyymmdd(dateString,locale := "Month first") (better than yyyymmdd(date) or )
(I don't see the problem with 10-12-20 or 12-10-20) - locale handle this cases as I can see.)
but the function can not handle 10122020 - to day...

Solution 1 .:
Is to further use a variable that tells how the date should prepare within it is finally processed (like this)
yyyymmdd(dateString, locale := "Month first", dateType := "1")
  • dateType = 1 (no handling)
  • dateType = 2 (convert Tue Aug 11 13:59:27 2020 to Aug 11 2020 before convert to 20200811 - locale doesn't matter?)
  • dateType = 3 (convert 12032020 to 120320 before convert to 20200312 - if locale := "Day first")
  • dateType = 4 ....
Solution 2 .: (more general?)
Always describe the date format to be handled e.g. ddmmyy or mmddyy or ddmmyyyy or .... (and skip Day first or Month first)
dateType1 and dateType2 may still need to be left to handle eg. Tue Aug 11 13:59:27 2020
A call could look like this .:

Code: Select all

Date := "10-12-20"
Date1 := yyyymmdd(Date, "ddmmyy") (or yyyymmdd(Date, "dd-mm-yy")
MsgBox % Date1

Date := "Tue Aug 11 13:59:27 2020"
Date2 := yyyymmdd(Date, "ddmmyy", "2") (or yyyymmdd(Date, "dd-mm-yy", "2")
; Maybe, the description [c]ddmmyy[/c] doesn't matter)
ExitApp

yyyymmdd(Date, ddmmyy, dateType := "1") (or yyyymmdd(Date, dd-mm-yy, dateType := "1")
{	mos := ....
      If (dateType := 2)
      .....
Is this complicated to perform?
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

18 Sep 2020, 07:15

Change the script as you wish. You do not need to modify the mos with your additions. Keep as is. You can move it into the function if you like. You can reorganize how it is displayed, but see the AHK documentation about continuation lines. Leave locale alone unless you are in Europe or another place where you typically show the day before the month; if so, just delete or comment the first locale line. You could change the script to accommodate ddmmyyyy; will need to think about how to distinguish from yyyymmdd formats. One would not typically need to have locale as a function parameter; I would not recommend that for most uses. Locale is just a way to tell the entire function whether day usually comes first, and that seldom varies in a given place (unless you are working with global data). If you have a string like "Aug 11 2020", the locale value will not be used because the day obviously follows the month in that case; the script will simply ignore the locale value in such cases (that is what md is handling). If you always know the date format, then you really don't need this script! :problem:
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

18 Sep 2020, 09:37

Updated to handle "05102020".

Code: Select all

; Parse the date
; @mikeyww on 18 September 2020
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=80896
; Version 03

Global locale
locale = Month first ; Change if not true
If (locale != "Month first")
 locale = Day first
dts := ["05 oct 2020"
 , "051020"
 , "09-10-20"
 , "13.10.2020"
 , "15-05-20"
 , "18.08.2020"
 , "19-8-2020"
 , "20.07.03"
 , "20-05-06"
 , "05102020"
 , "200730"
 , "200818"
 , "200820"
 , "200928"
 , "201005"
 , "2020/10/05"
 , "2020-10-05"
 , "20201013"
 , "30 juli 2020"
 , "5 okt 2020"
 , "5 oktober 2020"
 , "5-10-2020"
 , "Oct 5, 2020"]
For index, dt in dts {
 MsgBox, 1, % Format("#{} of {} ({})", index, dts.MaxIndex(), locale), % "Original:       " dt "`nyyyymmdd:   " yyyymmdd(dt)
 IfMsgBox, Cancel
  Break
}
ExitApp

yyyymmdd(dateString) {
 mos := {jan: 1, feb: 2, mar: 3, apr: 4, maj: 5, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, okt: 10, oct: 10, nov: 11, dec: 12}
 part := [], new := [], dt := StrReplace(dateString, ","), md := locale = "Month first" ? True : False
 If RegExMatch(dt, "^\d{2}[-.]\d{2}[-.]\d{2}$")
  dt := RegExReplace(dt, "[-.]")
 If RegExMatch(dt, "^\d{6}$") {
  f2 := SubStr(dt, 1, 2)
  If (f2 > 19)
   dt := f2 + 2000 SubStr(dt, 3)
  Else dt := SubStr(dt, 1, 4) Substr(dt, 5) + 2000
 }
 If RegExMatch(dt, "\d{8}")
  ff := (SubStr(dt, 1, 4) > 1999) * 2, dt := insert(dt, "-", ff + 3, ff + 5)
 part := StrSplit(dt, [A_Space, "/", "-", "."]) ; Get year, month, day
 For index, this in part
 {
  If this is alpha
  {
   For moName, moNum in mos
   {
    If SubStr(this, 1, 3) = moName ; Month matched a known string
     part[index] := moNum, md := index = 1 ? True : False ; If string starts with alpha month, then day follows it
   }
  }
 }
 For index, this in part
 {
  If !RegExMatch(this, "\d{4}") { ; This part is not the year
   md := this > 12 ? (index > 1 ? True : False) : md ; For numbers > 12, if it's second, then month precedes it
   new[new[3-md] ? md+2 : 3-md] := this ; Populate the month or day slot
  } Else new[1] := this, md := index = 1 ? True : md ; If year comes first, then the month follows it
 }
 Return Format("{}{:02}{:02}", new[1], new[2], new[3])
}

insert(string, char, p1, p2) {
 Return SubStr(string, 1, p1-1) char SubStr(string, p1, p2-p1) char SubStr(string, p2)
}
Albireo
Posts: 1821
Joined: 16 Oct 2013, 13:53

Re: Convert different formats for currency / numbers or date - to the same structure

20 Sep 2020, 14:38

Thank you!
It became a very good functional function()
Right now I will use this function() to analyze the dates in PDF files with xPDF

for the documentation, the function() is here:
A small example that converts dates to yyyymmdd with the function()
sokrakes
Posts: 42
Joined: 07 Aug 2019, 01:40

Re: Convert different formats for currency / numbers or date - to the same structure

05 Mar 2023, 09:49

Hi, I'm working on converting the date to numeric.
I have tried this and I haven't found the reason why it doesn't take accented fonts.
I'm doing a date conversion 1 září 2020 --> 20200901
But here I'm interested in the reason why it doesn't work

Jirka
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Convert different formats for currency / numbers or date - to the same structure

05 Mar 2023, 10:36

Code: Select all

; Parse the date
; @mikeyww on 18 September 2020
; Version 04
; 05 Mar 2023: Added: Allow Unicode in month names; match month name if first three letters do not match
; https://www.autohotkey.com/boards/viewtopic.php?p=510824#p510824
; Save this AutoHotkey script file as UTF-8 with BOM signature
#Requires AutoHotkey v1.1.33
Global locale := "Month first" ; Change if not true
(locale != "Month first") && locale := "Day first"
dts := ["1 září 2020"
      , "1 pro 2020"
      , "1 červenec 2020"
      , "1 červen 2020"
      , "05 oct 2020"
      , "051020"
      , "09-10-20"
      , "13.10.2020"
      , "15-05-20"
      , "18.08.2020"
      , "19-8-2020"
      , "20.07.03"
      , "20-05-06"
      , "05102020"
      , "200730"
      , "200818"
      , "200820"
      , "200928"
      , "201005"
      , "2020/10/05"
      , "2020-10-05"
      , "20201013"
      , "30 juli 2020"
      , "5 okt 2020"
      , "5 oktober 2020"
      , "5-10-2020"
      , "Oct 5, 2020"]
For index, dt in dts {
 MsgBox 1, % Format("#{} of {} ({})", index, dts.Count(), locale)
         , % "Original:       " dt "`nyyyymmdd:   " yyyymmdd(dt)
 IfMsgBox, Cancel
  Break
}
ExitApp

yyyymmdd(dateString) {
 Static unicodeLetter := "\p{L}"
  , mos := {jan:  1, led:  1, feb   :  2, úno:  2, mar     :  3, bře:  3, apr:  4, dub: 4, maj: 5, may: 5
          , kvě:  5, jun:  6, červen:  6, jul:  7, červenec:  7, aug:  8, srp:  8, sep: 9, zář: 9
          , okt: 10, oct: 10, říj   : 10, nov: 11, lis     : 11, dec: 12, pro: 12}
 part := [], new := [], dt := StrReplace(dateString, ","), md := locale = "Month first" ? True : False
 RegExMatch(dt, "^\d{2}[-.]\d{2}[-.]\d{2}$") && dt := RegExReplace(dt, "[-.]") ; Remove hyphens and dots
 If RegExMatch(dt, "^\d{6}$")
  f2 := SubStr(dt, 1, 2), dt := f2 > 19 ? f2 + 2000 SubStr(dt, 3) : SubStr(dt, 1, 4) Substr(dt, 5) + 2000
 If RegExMatch(dt, "\d{8}")
  ff := (SubStr(dt, 1, 4) > 1999) * 2, dt := insert(dt, "-", ff + 3, ff + 5)
 For index, thisPart in part := StrSplit(dt, [A_Space, "/", "-", "."])         ; Year, month, day
  If (thisPart ~= "i)^" unicodeLetter "+$")
   For moName, moNum in mos
    If SubStr(thisPart, 1, 3) = moName || moName = thisPart ; Month matched a known string
     part[index] := moNum, md := index = 1 ? True : False   ; If starts with alpha month, day follows it
 For index, thisPart in part
  If !RegExMatch(thisPart, "\d{4}") {                    ; This part is not the year
   md := thisPart > 12 ? (index > 1 ? True : False) : md ; For num > 12, if it's second, month precedes it
   new[new[3-md] ? md+2 : 3-md] := thisPart              ; Populate the month or day slot
  } Else new[1] := thisPart, md := index = 1 ? True : md ; If year comes first, then the month follows it
 Return Format("{}{:02}{:02}", new[1], new[2], new[3])
}

insert(string, char, p1, p2) {
 Return SubStr(string, 1, p1-1) char SubStr(string, p1, p2-p1) char SubStr(string, p2)
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 160 guests