Jump to content
Sign in to follow this  
SlowCoder74

Submitting my version of a date converter UDF

Recommended Posts

SlowCoder74

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

Share this post


Link to post
Share on other sites
Melba23

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

 

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
SlowCoder74

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.

Share this post


Link to post
Share on other sites
Melba23

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

 

Share this post


Link to post
Share on other sites
SlowCoder74

Just for the info:

In the Czech republic there is date format

dd.mm.yyyy

If what you're getting at is if my code can deal with it, the answer is yes.

Share this post


Link to post
Share on other sites
Zedna

If what you're getting at is if my code can deal with it, the answer is yes.

No it wasn't my intention. I reacted only on posts about different date format in various countries.

Share this post


Link to post
Share on other sites
Malkey

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

Share this post


Link to post
Share on other sites
SlowCoder74

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

Wow ... you realize you totally blew away my function with that? LOL! That's much more succinct! Thanks!

Share this post


Link to post
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
Sign in to follow this  

×