SlowCoder74 Posted May 28, 2012 Share Posted May 28, 2012 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? expandcollapse popupFunc _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 More sharing options...
Moderators Melba23 Posted May 28, 2012 Moderators Share Posted May 28, 2012 SlowCoder74,while people like to use "MM/DD/YYYYNot on this side of the Atlantic! We use DD/MM/YYYY! I have moved this to the Examples section. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
water Posted May 28, 2012 Share Posted May 28, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
SlowCoder74 Posted May 28, 2012 Author Share Posted May 28, 2012 SlowCoder74,Not on this side of the Atlantic! We use DD/MM/YYYY! I have moved this to the Examples section. M23LOL! 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 More sharing options...
Moderators Melba23 Posted May 28, 2012 Moderators Share Posted May 28, 2012 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Zedna Posted May 28, 2012 Share Posted May 28, 2012 (edited) Just for the info: In the Czech republic there is date format dd.mm.yyyy Edited May 28, 2012 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
SlowCoder74 Posted May 28, 2012 Author Share Posted May 28, 2012 Just for the info:In the Czech republic there is date formatdd.mm.yyyyIf what you're getting at is if my code can deal with it, the answer is yes. Link to comment Share on other sites More sharing options...
Zedna Posted May 29, 2012 Share Posted May 29, 2012 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. Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Malkey Posted May 29, 2012 Share Posted May 29, 2012 SlowCoder74Your _ConvertDateFormat() function works correctly and is easy to understand.ConsoleWrite(_ConvertDateFormat("1956/12/19","/", "Y/M/D","MM-DD-YYYY ") & @LF)returned12.19.1956For 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}")returns12.19.1956For 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 More sharing options...
SlowCoder74 Posted June 2, 2012 Author Share Posted June 2, 2012 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! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now