Code: Select all
; Parse the date
; @mikeyww on 17 September 2020
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=80896
#SingleInstance Force
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}
Date := "Tue Aug 11 13:59:27 2020"
vDate := yyyymmdd("Tue Aug 11 13:59:27 2020", "Month first")
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "30 juli 2020"
vDate := yyyymmdd(Date, "Day first")
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "09-10-20"
vDate := yyyymmdd(Date, "day First") ; dd-mm-yy
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "15-05-20"
vDate := yyyymmdd(Date, "Day First") ; dd-mm-yy
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "200820"
vDate := yyyymmdd(Date, "day First") ; ddmmyy
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "200730"
vDate := yyyymmdd(Date, "Month First") ; yymmdd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "200818"
vDate := yyyymmdd(Date, "Month First") ; yymmdd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "200928"
vDate := yyyymmdd(Date, "Month First") ; yymmdd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "20201013"
vDate := yyyymmdd(Date, "Month First")
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "20-05-06"
vDate := yyyymmdd(Date, "Month First") ; yy-mm-dd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "20.07.03"
vDate := yyyymmdd(Date, "Month First") ; yy.mm.dd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "18.08.2020"
vDate := yyyymmdd(Date, "day First")
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
Date := "13.10.2020"
vDate := yyyymmdd(Date, "Day First")
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: yyymmdd `n`n" Date "`n" vDate
; - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - -
Date := "Tue Aug 11 13:59:27 2020"
vDate := DateParse(Date)
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "30 juli 2020" ; OK
vDate := DateParse(Date)
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "09-10-20" ; OK!
vDate := DateParse(Date) ; dd-mm-yy
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "15-05-20" ; OK
vDate := DateParse(Date) ; dd-mm-yy
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "200820"
vDate := DateParse(Date) ; ddmmyy
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "200730"
vDate := DateParse(Date) ; yymmdd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "200818"
vDate := DateParse(Date) ; yymmdd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "200928"
vDate := DateParse(Date) ; yymmdd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "20201013"
vDate := DateParse(Date) ; yyyymmdd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "20-05-06"
vDate := DateParse(Date) ; yy-mm-dd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "20.07.03"
vDate := DateParse(Date) ; yy.mm.dd
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "18.08.2020" ; OK
vDate := DateParse(Date)
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
Date := "13.10.2020" ; OK!
vDate := DateParse(Date)
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "Function .: DateParse `n`n" Date "`n" vDate
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, Ready!
ExitApp
DateParse(str) {
static e2 = "i)(?:(\d{1,2}+)[\s\.\-\/,]+)?(\d{1,2}|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*)[\s\.\-\/,]+(\d{2,4})"
str := RegExReplace(str, "((?:" . SubStr(e2, 42, 47) . ")\w*)(\s*)(\d{1,2})\b", "$3$2$1", "", 1)
If RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?"
. "(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?"
. "(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i)
d3 := i1, d2 := i3, d1 := i4, t1 := i5, t2 := i7, t3 := i8
Else If !RegExMatch(str, "^\W*(\d{1,2}+)(\d{2})\W*$", t)
RegExMatch(str, "i)(\d{1,2})\s*:\s*(\d{1,2})(?:\s*(\d{1,2}))?(?:\s*([ap]m))?", t)
, RegExMatch(str, e2, d)
f = %A_FormatFloat%
SetFormat, Float, 02.0
d := (d3 ? (StrLen(d3) = 2 ? 20 : "") . d3 : A_YYYY)
. ((d2 := d2 + 0 ? d2 : (InStr(e2, SubStr(d2, 1, 3)) - 40) // 4 + 1.0) > 0
? d2 + 0.0 : A_MM) . ((d1 += 0.0) ? d1 : A_DD) . t1
+ (t1 = 12 ? t4 = "am" ? -12.0 : 0.0 : t4 = "am" ? 0.0 : 12.0) . t2 + 0.0 . t3 + 0.0
SetFormat, Float, %f%
Return, d
}
GetDateFormat(Date, Format := "dddd',' dd. MMMM yyyy")
{
VarSetCapacity(SYSTEMTIME, 16)
, NumPut(SubStr(Date, 1, 4), SYSTEMTIME, 0, "UShort")
, NumPut(SubStr(Date, 5, 2), SYSTEMTIME, 2, "UShort")
, NumPut(SubStr(Date, 7, 2), SYSTEMTIME, 6, "UShort")
size := DllCall("GetDateFormat", "UInt", 0x0400, "UInt", 0, "Ptr", &SYSTEMTIME, "Ptr", &Format, "Ptr", 0, "Int", 0)
VarSetCapacity(buf, size * (A_IsUnicode ? 2 : 1), 0)
if !(DllCall("GetDateFormat", "UInt", 0x0400, "UInt", 0, "Ptr", &SYSTEMTIME, "Ptr", &Format, "Str", buf, "Int", size))
return "*" A_LastError
return buf
}
; yyyymmdd(dateString, locale := "Month first" ){
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])
}