/* This DateStampConvert.ahk script demonstrates the DateConvert() function which converts and US or British formatted date into the DateTime Stamp for use in any of the AutoHotkey commands or functions. The function automatically recognizes four forms of the date: 1. US date format using text name of the month first (i.e. January 23, 2018 or Jan 23, 2018). 2. British date format using text name of the month second (i.e. 23 January 2018 or 23 Jan 2018). 3. US numeric date formats with month first and day of the month more than 12 (i.e. 1/23/2018, 1-23-18, etc.). 4. British numeric date formats month second and day of the month more than 12 (i.e. 23/1/2018, 23-1-18, etc.). When both the month number and day of the month are 12 or below, the date format becomes ambiguous. The script asks the user to pick a format (US or British) to clear up the confusion. Select the formatted date in any document or Web page then press CTRL+ALT+WIN+d. The message box displays the DateTime Stamp (e.g. 20181019). When using DateStampConvert.ahk in conjunction with the HowLongYearsMonthsDays.ahk script, you can select dates in documents and feed them in DateStamp format directly into the input fields. You must include the two routines in the same script. */ Global Format RegRead, Format, HKCU\Software\DateStampConvert, Format If ErrorLevel = 1 { RegWrite, REG_SZ, HKCU\Software\DateStampConvert, Format, American Format := "American" } RegWrite, REG_SZ, HKCU\Software\DateStampConvert, Test1, 🦄 RegWrite, REG_EXPAND_SZ, HKCU\Software\DateStampConvert, Test2, 🦄 ; RegRead, Format, HKCU, Software\DateStampConvert, Format ; RegWrite, REG_SZ, HKCU, Software\DateStampConvert, American, 1 Menu, Tray, Add , % "Default Format " Format " (Change)", ChangeDefault msgbox format %format% ; Return ChangeDefault: If (Format = "American") { RegWrite, REG_SZ, HKCU\Software\DateStampConvert, Format,British Format := "British" Menu, Tray, Rename, Default Format American (Change), Default Format British (Change) } Else { RegWrite, REG_SZ, HKCU\Software\DateStampConvert, Format,American Format := "American" Menu, Tray, Rename, Default Format British (Change), Default Format American (Change) } Return ; This Hotkey tests the convert function and sends StartDate (if #Include ; DateStampConvert.ahk placed toward the end of the target script) ^!#d:: StartDate := DateConvert() ; MsgBox, % StartDate ; If #Included in the HowLongYearsMonthsDays.ahk script GuiControl, SpanCalc: , SysDateTimePick321, %StartDate% Return ; This Hotkey tests the convert function and sends StopDate (if #Include ; DateStampConvert.ahk placed toward the end of the target script) ^!#z:: StopDate := DateConvert() ; MsgBox, % StopDate ; If #Included in the HowLongYearsMonthsDays.ahk script GuiControl, SpanCalc: , SysDateTimePick322, %StopDate% Return ; Function for converting selected dates into DateTime Stamp. DateConvert() { ; Standard AutoHotkey Clipboard Routine OldClipboard:= ClipboardAll Clipboard:= "" Send, ^c ;copies selected text ClipWait 0 If ErrorLevel { MsgBox, No Text Selected! Return } Clipboard := Trim(Clipboard) ; Regular Expression to identify US date format with alphabetic month Octob 10 2018 If (RegExMatch(Clipboard, "^([[:alpha:]äéû]+).*?(\d\d?).*?(\d\d\d?\d?)$" , Date)) { ; Convert alphabetic month to numeric month /* ; Clock runttime DllCall("QueryPerformanceFrequency", "Int64*", freq) DllCall("QueryPerformanceCounter", "Int64*", CounterBefore) */ NewMonth := MonthConvert(Date1) /* DllCall("QueryPerformanceCounter", "Int64*", CounterAfter) MsgBox % "Elapsed QPC time is " . (CounterAfter - CounterBefore) / freq * 1000 " ms" */ If NewMonth = "Not found!" { MsgBox Not a valid date! Return } Else Return MakeStamp(Date3,NewMonth,Date2) } ; British date formats 1 October 2018 Else If (RegExMatch(Clipboard, "^(\d\d?).*?([[:alpha:]äéû]+).*?(\d\d\d?\d?)$" , Date)) { ; Convert alphabetic month to numeric month NewMonth := MonthConvert(Date2) If NewMonth = "Not found!" { MsgBox Not a valid date! Exit } Else Return MakeStamp(Date3,NewMonth,Date1) } Else If (RegExMatch(Clipboard, "^(\d\d?).*?(\d\d?).*?(\d\d\d?\d?)$" , Date)) { If (Date1 > 12) Return MakeStamp(Date3,Date2,Date1) ; 13-10-18 Else If (Date2 > 12) Return MakeStamp(Date3,Date1,Date2) ; 10-13-2018 Else { ; ambiguous date format MsgBox, 4,, US date format? (press Yes)`rBritish date format? (press No) If (Format = "American") Return MakeStamp(Date3,Date1,Date2) ; 10-1-18 else Return MakeStamp(Date3,Date2,Date1) ; 1-10-18 } } Else MsgBox No date found! Clipboard := OldClipboard } ; This function uses nested ternary operators to convert text months into numbers. /* MonthConvert(month) { NewMonth := InStr(month, "jan") ? "01" : InStr(month, "feb") ? "02" : InStr(month, "mar") ? "03" : InStr(month, "apr") ? "04" : InStr(month, "may") ? "05" : InStr(month, "jun") ? "06" : InStr(month, "jul") ? "07" : InStr(month, "aug") ? "08" : InStr(month, "sep") ? "09" : InStr(month, "oct") ? "10" : InStr(month, "nov") ? "11" : InStr(month, "dec") ? "12" :"Not found!" Return NewMonth } */ ;/* MonthConvert(month) { ; MsgBox % SubStr(month,1,3) Switch SubStr(month,1,3) { Case "jan","ene","jän","gen":Return "01" Case "feb","fév","fev": Return "02" Case "mar","mär": Return "03" Case "apr","abr","avr": Return "04" Case "may","mai","mag": Return "05" Case "jun","jui","gui": ; MsgBox If (SubStr(month,1,4) = "juil") Return "07" Else Return "06" Case "jul","lug": Return "07" Case "aug","ago","aoû","aou","ag": Return "08" Case "sep","set": Return "09" Case "oct","okt","ott": Return "10" Case "nov": Return "11" Case "dec","dic","dez","déc": Return "12" } } ;*/ ; This function is a little arbitrary since it cuts off at year 1940 before jumping to the 2000's YearCheck(ByRef Year) { If Year > 40 Year := "19" . Year Else Year := "20" . Year } MakeStamp(Year,Month,Day) { ; if two-digit year, convert to four-digit year If StrLen(Year) = 2 YearCheck(Year) ; concatenate DateTime Stamp DateStamp := Year . Format("{:02}", Month) . Format("{:02}", Day) ; Check for valid date If DateStamp is not Date { MsgBox Invalid date! %DateStamp% Exit } Else Return DateStamp }