Converting Formatted Dates (e.g. US or British) in Documents into the Standard DateTime Stamp (yyyymmdd)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jackdunning
Posts: 126
Joined: 01 Aug 2016, 18:17
Contact:

Converting Formatted Dates (e.g. US or British) in Documents into the Standard DateTime Stamp (yyyymmdd)

20 Nov 2018, 18:38

I wrote this date conversion function to use in conjunction with the timespan calculation function discussed in the
Function Calculating Timespan in Years, Months, and Days. This DateConvert() function converts any US or British formatted date into the DateTime Stamp (i.e. yyyymmdd) for use with AutoHotkey commands, functions, and date type GUI controls accepting this type of input. The function automatically recognizes four date formats:

1. US date format placing the text name of the month first (i.e. January 23, 2018 or Jan 23, 2018).
2. British date format placing the text name of the month second (i.e. 23 January 2018 or 23 Jan 2018).
3. US numeric date formats with the month first and any day of the month more than 12 (i.e. 1/23/2018, 1-23-18, etc.).
4. British numeric date formats with the month second and any day of the month more than 12 (i.e. 23/1/2018, 23-1-18, etc.).

When using the month number and a day of the month 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).

Image

For a more detailed discussion of the function, see the blogs, "Using Regular Expressions to Convert Most Formatted Dates into DateTime Stamps", "Use the Ternary Operator to Create Conditional Case Statements or Switches ", and "Wrapping Up the DateStampConvert.ahk Script "

When using DateConvert() in conjunction with the HowLongYearsMonthsDays.ahk script, you can select dates in documents and feed them in the
DateStamp format directly into the input fields.

Image

For more information on the HowLongYearsMonthsDays.ahk script see this Web page.

The following code does not contain the commands required to link to the HowLongYearsMonthsDays.ahk script, but you can find the current version with both Hotkeys and appropriate commands in the ConvertDateStamp.ahk script at my Free AutoHotkey Scripts page.

Code: Select all

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
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 (e.g. October 10, 2018)

If (RegExMatch(Clipboard, "^([[:alpha:]]+).*?(\d\d?).*?(\d\d\d?\d?)$" , Date))
{

; Convert alphabetic month to numeric month
    NewMonth := MonthConvert(Date1)
    If NewMonth = "Not found!"
    {
      MsgBox Not a valid date!
      Return
    }
    Else
      Return MakeStamp(Date3,NewMonth,Date2)

}

; British date formats  (e.g. 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)
IfMsgBox Yes
      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
}

; 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
}
If you have a need to extract various formatted dates from documents in DateTime Stamp format, then you might find this function useful.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Converting Formatted Dates (e.g. US or British) in Documents into the Standard DateTime Stamp (yyyymmdd)

25 Nov 2018, 03:53

Excellent function. Will be put to immediate use undoubtedly. :)

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: ositoMalvado and 116 guests