Jump to content

Submitting my version of a date converter UDF


 Share

Recommended Posts

Would like to know what you think of it. It's pretty well capable of converting any numeric-based date format to any other. They don't even have to be standard date formats.

The reason I coded it is that I find that AutoIT dates are often formatted as "YYYY/MM/DD", while people like to use "MM/DD/YYYY". Very often I prefer to use "YYYYMMDD" in my code or data. And sometimes there are other formats.

I'd appreciate comments, including if you feel the documentation is clear. Is it useful?

Func _ConvertDateFormat($sDateToConvert,$sOrigDelimiter,$sOrigDateMask,$sNewDateMask)
;Function to convert any numeric date format to any other numeric date format.
;$sDateToConvert - Any numeric date format
;sOrigDelimiter - Delimiter in $sDateToConvert that separates each part of the date.  Usually "/" or "-".
;$sOrigDateMask - Format changes, and depends on whether $sOrigDelimiter is empty or not.
; if $sOrigDelimiter = non-empty value: $sOrigDateMask will be something like "Y/M/D".  Single letters denoting which part of the string are year,month,day respectively.
; if $sOrigDelimiter = empty value: $sOrigDateMask must match the format of $sDateToConvert.  E.g.  If $sDateToConvert is "2012/05/28", then $sOrigDateMask must be "YYYY/MM/DD".
;$sNewDateMask - New masked format to be returned.  E.g. "MM/DD/YYYY" (returns "05/28/2012") or "YYYYMMDD" (returns "20120528")
; Will also accept YY for 2 digit year output, M for 1 digit month (if able), and D for 1 digit day (if able).  E.g. "M-D-YY" for "01/01/2012" will reformat to "1-1-12"
;Ex: $d = ConvertDateFormat("2012/05/28","Y/M/D","/","MM-DD-YYYY") - will convert "2012/05/28" to "05-28-2012"
;Ex: $d = ConvertDateFormat("20120528","YYYYMMDD","","MM-DD-YYYY") - will convert "20120528" to "05-28-2012"
;Note: if the year, month or day parts are not retrieved from $sDateToConvert, function will assume current value for that part.
;Note: The current date can even be returned in any format using the following: _ConvertDateFormat("","","","YYYYMMDD")
local $sYear
local $sMonth
local $sDay
local $aDateParts,$aDateMaskParts
local $iA
if $sOrigDelimiter <> "" then ;delimiter supplied.
  $aDateParts = StringSplit($sDateToConvert,$sOrigDelimiter) ;split original date into parts
  $aDateMaskParts = StringSplit($sOrigDateMask,$sOrigDelimiter) ;split original date mask into parts
  for $iA = 1 to $aDateParts[0]
   Switch StringUpper($aDateMaskParts[$iA])
    case "M" ;month
     $sMonth = StringStripWS($aDateParts[$iA],3)
    case "D" ;day
     $sDay = StringStripWS($aDateParts[$iA],3)
    case "Y" ;year
     $sYear = StringStripWS($aDateParts[$iA],3)
   EndSwitch
  Next
  Else ;delimiter not supplied.  Use mask to manually pull parts
  ;parse into year, month, day
  for $iA = 1 to StringLen($sDateToConvert)
   $sCurChar = stringupper(stringmid($sDateToConvert,$iA,1))
   switch stringupper(stringmid($sOrigDateMask,$iA,1))
    case "M" ;month
     $sMonth = $sMonth & $sCurChar
    case "D" ;day
     $sDay = $sDay & $sCurChar
    case "Y" ;year
     $sYear = $sYear & $sCurChar
   EndSwitch
  Next
EndIf
;fix length of date parts
;year
if $sYear = "" then $sYear = @YEAR
if stringlen($sYear) = 2 then $sYear = "20" & $sYear ;adjust year to 4 digits
;month
if $sMonth = "" then $sMonth = @MON
if stringlen($sMonth) = 1 then $sMonth = "0" & $sMonth ;adjust month to 2 digits
;day
if $sDay = "" then $sDay = @MDAY
if stringlen($sDay) = 1 then $sDay = "0" & $sDay ;adjust day to 2 digits
;create new format
$sNewDateMask = StringReplace($sNewDateMask,"YYYY",$sYear) ;apply 4-digit year
$sNewDateMask = StringReplace($sNewDateMask,"YY",StringRight($sYear,2)) ;apply 2-digit year
$sNewDateMask = StringReplace($sNewDateMask,"MM",$sMonth) ;apply 2-digit month
if StringInStr($sNewDateMask,"M") > 0 Then $sNewDateMask = StringReplace($sNewDateMask,"M",int($sMonth)) ;apply 1-digit month
$sNewDateMask = StringReplace($sNewDateMask,"DD",$sDay) ;apply 2-digit day
if StringInStr($sNewDateMask,"D") > 0 Then $sNewDateMask = StringReplace($sNewDateMask,"D",int($sDay)) ;apply 1-digit day
;return new format
Return $sNewDateMask
EndFunc
Link to comment
Share on other sites

  • Moderators

SlowCoder74,

while people like to use "MM/DD/YYYY

Not on this side of the Atlantic! We use DD/MM/YYYY! :)

I have moved this to the Examples section. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Looks good ;)

If you need some ideas for further enhancement here is a that has some additional features.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

SlowCoder74,

Not on this side of the Atlantic! We use DD/MM/YYYY! :)

I have moved this to the Examples section. ;)

M23

LOL! Do you also live where they drive cars on the wrong side of the road?

As an example where this can be useful ...

I'm using ShowPopupCalendar (a great little UDF), it returns the date in "MM/DD/YYYY" format. Whereas, I need to run _DateDiff on it, which accepts the date as "YYYY/MM/DD". Seems a bit of inconsistency in the published functions, but that is where my code comes into play.

Link to comment
Share on other sites

  • Moderators

SlowCoder74,

Do you also live where they drive cars on the wrong side of the road?

No, where I live we drive cars on the correct side of the road - which could in fact be either side. The important thing is that we all drive on the same side when going in the [b[same direction! ;)

And I was not criticizing the function, just the comment. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

SlowCoder74

Your _ConvertDateFormat() function works correctly and is easy to understand.

ConsoleWrite(_ConvertDateFormat("1956/12/19","/", "Y/M/D","MM-DD-YYYY ") &amp; @LF)

returned

12.19.1956

For simple date conversions like this, I would use a one liner Regular expression replace function.

StringRegExpReplace("1956/12/19", "(\d{4})/(\d{2})/(\d{2})", "${2}.${3}.${1}")

returns

12.19.1956

For a complex date/time conversion, like say

"2012/05/29"

to

"Tuesday, May 29, 2012 "

I would use

_Date_Time_Convert("2012/05/29", "yyyy/MM/dd", "dddd, MMM dd, yyyy")

where the _Date_Time_Convert() function can be found

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...