AdamUL Posted November 11, 2011 Share Posted November 11, 2011 (edited) Here is a UDF that I created when I had to do a large project working with dates and times, and calculations with them. I need to convert the time from 12 hr time with "AM" and "PM", and sometimes with "a" and "p", to 24 hr time for calculations, and convert the time back to 12 hr when needed. I also needed to convert "MM/DD/YYYY" to "YYYY/MM/DD" for use in calculations as well, and convert back as well.For details on each function, please look at the function headers.Look at Melba23's more flexible Date_Time_Convert UDF. I use that UDF more now than this one, but I still use "IsBetween" functions in this UDF. DateTimeConvert.au3expandcollapse popup#include-once #include <Date.au3> #AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y ; #INDEX# ======================================================================================================================= ; Title .........: Time and Date Conversion Library ; AutoIt Version : 3.3.6++ ; UDF Version ...: 1.1 ; Language ......: English ; Description ...: Converts time between 12 hr and 24 hr with other time and date related functions. ; Dll ...........: ; Author(s) .....: Adam Lawrence (AdamUL) ; Email .........: ; Modified.......: ; Contributors ..: ; Resources .....: ; Remarks .......: ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_IsTime12Hr ;_IsTime24Hr ;_Time12HrTo24Hr ;_Time24HrTo12Hr ;_IsCalcDate ;_IsStandardDate ;_IsDateAndTime ;_DateStandardToCalcDate ;_DateCalcToStandardDate ;_IsBetweenTimes ;_IsBetweenDatesTimes ;_IsBetweenDatesTimesLite ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsTime12Hr ; Description ...: Checks to see if a time string is in the 12 hr (AM/PM) format. ; Syntax ........: _IsTime12Hr($sTime) ; Parameters ....: $sTime - A string value in time format. ; Return values .: Success - True ; Failure - False, sets @error to: ; |0 - String is not in 12 hr time format. ; |1 - Invalid time format string. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsTime12Hr($sTime) If StringRegExp($sTime, "^(1[0-2]|[1-9]):([0-5]\d):?([0-5]\d)?(?-i:\h*)(?i)([ap]m?)$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsTime24Hr ; Description ...: Checks to see if a time string is in the 24 hr format. ; Syntax ........: _IsTime24Hr($sTime) ; Parameters ....: $sTime - A string value in time format. ; Return values .: Success - True ; Failure - False, sets @error to: ; |0 - String is not in 24 hr time format. ; |1 - Invalid time format string. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsTime24Hr($sTime) If StringRegExp($sTime, "^([01]?\d|2[0-3]):([0-5]\d):?([0-5]\d)?$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Time12HrTo24Hr ; Description ...: Convert 12 hr (AM/PM) time string to a 24 hr time string. ; Syntax ........: _Time12HrTo24Hr($sTime[, $fDisplaySecs = True[, $fHourLeadingZero = False]]) ; Parameters ....: $sTime - "hh:mm:ss AM/PM" time format. ; $fDisplaySecs - [optional] A boolean value to display seconds values. Default is True. ; $fHourLeadingZero - [optional] A boolean value to pad leading zero to single digit hours. Default is False. ; Return values .: Success - A string value in "hh:mm:ss" 24 hr time string. ; Failure - "", sets @error to: ; |1 - Invalid time format string ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: _Time24HrTo12Hr ; Link ..........: ; Example .......: _Time12HrTo24Hr("12:30AM"), _Time12HrTo24Hr("1:30:45 PM"), _Time12HrTo24Hr("12:30 pm") ; =============================================================================================================================== Func _Time12HrTo24Hr($sTime, $fDisplaySecs = True, $fHourLeadingZero = False) Local $aTime = StringRegExp($sTime, "^(1[0-2]|[1-9]):([0-5]\d):?([0-5]\d)?(?-i:\h*)(?i)([ap]m?)$", 1) If @error Then Return SetError(1, 0, "") Local $sHour = $aTime[0] Local $sMins = $aTime[1] Local $sSecs = $aTime[2] Local $sAMPM = $aTime[3] $sHour = Mod($sHour, 12) If StringInStr($sAMPM, "p") Then $sHour += 12 If $fHourLeadingZero And Number($sHour) < 10 And StringLen($sHour) = 1 Then $sHour = "0" & $sHour If $fDisplaySecs And $sSecs = "" Then $sSecs = "00" If $fDisplaySecs Then Return $sHour & ":" & $sMins & ":" & $sSecs Return $sHour & ":" & $sMins EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Time24HrTo12Hr ; Description ...: Convert 24 hr time string to a 12 hr (AM/PM) time string. ; Syntax ........: _Time24HrTo12Hr($sTime[, $fDisplaySecs = True[, $fHourLeadingZero = False[, $sAMPMDelim = " "]]]) ; Parameters ....: $sTime - A string value in "hh:mm:ss" time format. ; $fDisplaySecs - [optional] A boolean value to display seconds values. Default is True. ; $fHourLeadingZero - [optional] A boolean value to pad leading zero to single digit hours. Default is False. ; $sAMPMDelim - [optional] A string value delimiter to seperate AM/PM from the numeric time. Default is " ". ; Return values .: Success - "hh:mm:ss AM/PM" time format. ; Failure - "", sets @error to: ; |1 - Invalid time format string. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: _Time12HrTo24Hr ; Link ..........: ; Example .......: _Time24HrTo12Hr("0:30"), _Time24HrTo12Hr("15:45:36"), _Time24HrTo12Hr("5:36") ; =============================================================================================================================== Func _Time24HrTo12Hr($sTime, $fDisplaySecs = True, $fHourLeadingZero = False, $sAMPMDelim = " ") Local $aTime = StringRegExp($sTime, "^([01]?\d|2[0-3]):([0-5]\d):?([0-5]\d)?$", 1) If @error Then Return SetError(1, 0, "") If UBound($aTime) = 2 Then ReDim $aTime[3] Local $sHour = $aTime[0] Local $sMins = $aTime[1] Local $sSecs = $aTime[2] Local $sAMPM = "" Switch $sHour Case 0 $sHour = 12 $sAMPM = "AM" Case 1 To 11 $sAMPM = "AM" Case 12 $sAMPM = "PM" Case 13 To 23 $sHour = $sHour - 12 $sAMPM = "PM" Case Else EndSwitch If $fHourLeadingZero And Number($sHour) < 10 And StringLen($sHour) = 1 Then $sHour = "0" & $sHour If Not $fHourLeadingZero And Number($sHour) < 10 And StringLen($sHour) = 2 Then $sHour = Number($sHour) If $fDisplaySecs And $sSecs = "" Then $sSecs = "00" If $fDisplaySecs Then Return $sHour & ":" & $sMins & ":" & $sSecs & $sAMPMDelim & $sAMPM Return $sHour & ":" & $sMins & $sAMPMDelim & $sAMPM EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsCalcDate ; Description ...: Checks to see if a date is in a format for calculations. ; Syntax ........: _IsCalcDate($sDate) ; Parameters ....: $sDate - A string value date format. ; Return values .: Success - True ; Failure - False, sets @error to 1. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsCalcDate($sDate) If StringRegExp($sDate, "^(\d{4})/(\d{1,2})/(\d{1,2})$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsStandardDate ; Description ...: Checks to see if a date is in a standard date format, "MM/DD/YYYY". ; Syntax ........: _IsStandardDate($sDate) ; Parameters ....: $sDate - A string value in date format. ; Return values .: Success - True ; Failure - False, sets @error to 1. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsStandardDate($sDate) If StringRegExp($sDate, "^(\d{1,2})/(\d{1,2})/(\d{4})$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsDateAndTime ; Description ...: Checks to see if a string is in a date and time format. ; Syntax ........: _IsDateAndTime($sDateTime) ; Parameters ....: $sDateTime - A string value in date and time format. ; Return values .: Success - True ; Failure - False, sets @error to 1. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsDateAndTime($sDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" If StringRegExp($sDateTime, $sRegEx) Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateStandardToCalcDate ; Description ...: Convert a date from "MM/DD/YYYY" to "YYYY/MM/DD" format to use in date calculations. ; Syntax ........: _DateStandardToCalcDate($sDate) ; Parameters ....: $sDate - A string value in "MM/DD/YYYY" format. ; Return values .: Success - Calc date string. ; Failure - "", sets @error to: ; |1 - Invalid date format. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DateStandardToCalcDate($sDate) If Not StringRegExp($sDate, "^(\d{1,2})/(\d{1,2})/(\d{4})$") Then Return SetError(1, 0, "") If @error Then Return SetError(1, 0, "") Local $sDateNew = StringRegExpReplace($sDate, "(\d{2})/(\d{2})/(\d{4})", "$3/$1/$2") $sDateNew = StringRegExpReplace($sDateNew, "(\d{2})/(\d)/(\d{4})", "$3/$1/0$2") $sDateNew = StringRegExpReplace($sDateNew, "(\d)/(\d{2})/(\d{4})", "$3/0$1/$2") $sDateNew = StringRegExpReplace($sDateNew, "(\d)/(\d)/(\d{4})", "$3/0$1/0$2") Return $sDateNew EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateCalcToStandardDate ; Description ...: Convert a date from "YYYY/MM/DD" to "MM/DD/YYYY" format. ; Syntax ........: _DateCalcToStandardDate($sDate) ; Parameters ....: $sDate - A string value in "YYYY/MM/DD" format. ; Return values .: Success - Standard date string ; Failure - "", sets @error to: ; |1 - Invalid date format. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DateCalcToStandardDate($sDate) Local $aDate = StringRegExp($sDate, "^(\d{4})/(\d{1,2})/(\d{1,2})$", 1) If @error Then Return SetError(1, 0, "") Local $sYear = $aDate[0] Local $sMonth = $aDate[1] Local $sDay = $aDate[2] Return Number($sMonth) & "/" & Number($sDay) & "/" & $sYear EndFunc Func _DateTimeStandardToCalcDateTime($sDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" Local $aDateTime = StringRegExp($sDateTime, $sRegEx, 1) If @error Then Return SetError(1, 1, "") Local $sDate = $aDateTime[0] Local $sTime = $aDateTime[1] If _IsStandardDate($sDate) Then $sDate = _DateStandardToCalcDate($sDate) If @error Then Return SetError(2, 1, "") EndIf If Not _IsCalcDate($sDate) Then Return SetError(2, 2, "") If _IsTime12Hr($sTime) Then $sTime = _Time12HrTo24Hr($sTime) If @error Then Return SetError(3, 1, "") EndIf If Not _IsTime24Hr($sTime) Then Return SetError(3, 2, "") Return $sDate & " " & $sTime EndFunc Func _DateTimeCalcToStandardDateTime($sDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" Local $aDateTime = StringRegExp($sDateTime, $sRegEx, 1) If @error Then Return SetError(1, 1, "") Local $sDate = $aDateTime[0] Local $sTime = $aDateTime[1] If _IsCalcDate($sDate) Then $sDate = _DateCalcToStandardDate($sDate) If @error Then Return SetError(2, 1, "") EndIf If Not _IsStandardDate($sDate) Then Return SetError(2, 2, "") If _IsTime24Hr($sTime) Then $sTime = _Time24HrTo12Hr($sTime) If @error Then Return SetError(3, 1, "") EndIf If Not _IsTime12Hr($sTime) Then Return SetError(3, 2, "") Return $sDate & " " & $sTime EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsBetweenTimes ; Description ...: Test a time to see if it is between the Start Time and the End Time in a 24 hour day. ; Syntax ........: _IsBetweenTimes($sTestTime, $sStartTime, $sEndTime) ; Parameters ....: $sTestTime - A string value time to test, in 12 hr or 24 hr format. ; $sStartTime - A string value start Time, must be before End Time in 12 hr or 24 hr format. ; $sEndTime - A string value end Time, must be after Start Time in 12 hr or 24 hr format. ; Return values .: Success - True ; Failure - False, sets @error to: ; |0 - Not between times. ; |1 - Invalid 12 Hr format. ; |2 - Invalid 24 Hr format. ; |3 - Invalid time string. ; |4 - End Time before Start Time. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsBetweenTimes($sTestTime, $sStartTime, $sEndTime) If _IsTime12Hr($sTestTime) Then $sTestTime = _Time12HrTo24Hr($sTestTime) If @error Then Return SetError(1, 1, False) EndIf If _IsTime12Hr($sStartTime) Then $sStartTime = _Time12HrTo24Hr($sStartTime) If @error Then Return SetError(1, 2, False) EndIf If _IsTime12Hr($sEndTime) Then $sEndTime = _Time12HrTo24Hr($sEndTime) If @error Then Return SetError(1, 3, False) EndIf If Not _IsTime24Hr($sTestTime) Then Return SetError(2, 1, False) If Not _IsTime24Hr($sStartTime) Then Return SetError(2, 2, False) If Not _IsTime24Hr($sEndTime) Then Return SetError(2, 3, False) $sTestTime = StringReplace(StringStripWS($sTestTime, 8), ":", "") If @error Or @extended > 2 Then Return SetError(3, 1, False) If @extended = 1 Then $sTestTime &= "00" Local $iTestTime = Number($sTestTime) $sStartTime = StringReplace(StringStripWS($sStartTime, 8), ":", "") If @error Or @extended > 2 Then Return SetError(3, 2, False) If @extended = 1 Then $sStartTime &= "00" Local $iStartTime = Number($sStartTime) $sEndTime = StringReplace(StringStripWS($sEndTime, 8), ":", "") If @error Or @extended > 2 Then Return SetError(3, 3, False) If @extended = 1 Then $sEndTime &= "00" Local $iEndTime = Number($sEndTime) If $iEndTime < $iStartTime Then Return SetError(4, 0, False) If $iTestTime >= $iStartTime And $iTestTime <= $iEndTime Then Return True Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsBetweenDatesTimes ; Description ...: Test a time to see if it is between the Start Date and Time and the End Date and Time. ; Syntax ........: _IsBetweenDatesTimes($sTestDateTime, $sStartDateTime, $sEndDateTime) ; Parameters ....: $sTestDateTime - A string value, Date and Time to test, in 12 hr or 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sStartDateTime - A string value, Start Date and Time, must be before End Date and Time in 12 hr or 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sEndDateTime - A string value, End Date and Time, must be after Start Date and Time in 12 hr or 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; Return values .: Success - True ; Failure - False, sets @error to: ; |1 - Invalid date format. ; |2 - Error Converting to Calc date. ; |3 - Invalid time format. ; |4 - Invalid $sTestDateTime ; |5 - Invalid $sStartDateTime ; |6 - Invalid $sEndDateTime ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: _IsBetweenDatesTimesLite, _IsBetweenTimes ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsBetweenDatesTimes($sTestDateTime, $sStartDateTime, $sEndDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" Local $aTestDateTime = StringRegExp($sTestDateTime, $sRegEx, 1) If @error Then Return SetError(1, 1, False) Local $sTestDate = $aTestDateTime[0] Local $sTestTime = $aTestDateTime[1] Local $aStartDateTime = StringRegExp($sStartDateTime, $sRegEx, 1) If @error Then Return SetError(1, 2, False) Local $sStartDate = $aStartDateTime[0] Local $sStartTime = $aStartDateTime[1] Local $aEndDateTime = StringRegExp($sEndDateTime, $sRegEx, 1) If @error Then Return SetError(1, 3, False) Local $sEndDate = $aEndDateTime[0] Local $sEndTime = $aEndDateTime[1] Select Case $sTestDate = "" And $sStartDate = "" And $sEndDate = "" $sTestDate = _NowCalcDate() $sStartDate = $sTestDate $sEndDate = $sTestDate Case $sTestDate <> "" And $sStartDate <> "" And $sEndDate <> "" Case $sTestDate = "" And $sStartDate <> "" And $sEndDate <> "" ContinueCase Case $sTestDate <> "" And $sStartDate = "" And $sEndDate <> "" ContinueCase Case $sTestDate <> "" And $sStartDate <> "" And $sEndDate = "" ContinueCase Case Else Return SetError(1, 4, False) EndSelect If _IsStandardDate($sTestDate) Then $sTestDate = _DateStandardToCalcDate($sTestDate) If @error Then Return SetError(2, 1, False) EndIf If _IsStandardDate($sStartDate) Then $sStartDate = _DateStandardToCalcDate($sStartDate) If @error Then Return SetError(2, 2, False) EndIf If _IsStandardDate($sEndDate) Then $sEndDate = _DateStandardToCalcDate($sEndDate) If @error Then Return SetError(2, 3, False) EndIf If Not _IsCalcDate($sTestDate) Then Return SetError(3, 1, False) If Not _IsCalcDate($sStartDate) Then Return SetError(3, 2, False) If Not _IsCalcDate($sEndDate) Then Return SetError(3, 3, False) $sTestDate = $sTestDate & " " $sStartDate = $sStartDate & " " $sEndDate = $sEndDate & " " If _IsTime12Hr($sTestTime) Then $sTestTime = _Time12HrTo24Hr($sTestTime) If @error Then Return SetError(2, 4, False) EndIf If _IsTime12Hr($sStartTime) Then $sStartTime = _Time12HrTo24Hr($sStartTime) If @error Then Return SetError(2, 5, False) EndIf If _IsTime12Hr($sEndTime) Then $sEndTime = _Time12HrTo24Hr($sEndTime) If @error Then Return SetError(2, 6, False) EndIf If Not _IsTime24Hr($sTestTime) Then Return SetError(3, 4, False) If Not _IsTime24Hr($sStartTime) Then Return SetError(3, 5, False) If Not _IsTime24Hr($sEndTime) Then Return SetError(3, 6, False) $sTestDateTime = $sTestDate & $sTestTime $sStartDateTime = $sStartDate & $sStartTime $sEndDateTime = $sEndDate & $sEndTime Local $sStartTestDateTimeDiff = _DateDiff("s", $sStartDateTime, $sTestDateTime) Switch @error Case 2 Return SetError(5, @error, False) Case 3 Return SetError(4, @error, False) EndSwitch Local $sEndTestDateTimeDiff = _DateDiff("s", $sTestDateTime, $sEndDateTime) Switch @error Case 2 Return SetError(4, @error, False) Case 3 Return SetError(6, @error, False) EndSwitch If $sStartTestDateTimeDiff >= 0 And $sEndTestDateTimeDiff >= 0 Then Return True Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsBetweenDatesTimesLite ; Description ...: Test a time to see if it is between the Start Date and Time and the End Date and Time. ; Syntax ........: _IsBetweenDatesTimesLite($sTestDateTime, $sStartDateTime, $sEndDateTime) ; Parameters ....: $sTestDateTime - A string value, Date and Time to test, in 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sStartDateTime - A string value, Start Date and Time, must be before End Date and Time in 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sEndDateTime - A string value, End Date and Time, must be after Start Date and Time in 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; Return values .: Success - True ; Failure - False, sets @error to: ; |1 - Invalid sTestDateTime. ; |2 - Invalid $sStartDateTime. ; |3 - Invalid $sEndDateTime. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: Faster return than the _IsBetweenDatesTimes function, but more retricted on the format of the date and time entered. ; Related .......: _IsBetweenDatesTimes, _IsBetweenTimes ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsBetweenDatesTimesLite($sTestDateTime, $sStartDateTime, $sEndDateTime) Local $sStartTestDateTimeDiff = _DateDiff("s", $sStartDateTime, $sTestDateTime) Switch @error Case 2 Return SetError(2, @error, False) Case 3 Return SetError(1, @error, False) EndSwitch Local $sEndTestDateTimeDiff = _DateDiff("s", $sTestDateTime, $sEndDateTime) Switch @error Case 2 Return SetError(1, @error, False) Case 3 Return SetError(3, @error, False) EndSwitch If $sStartTestDateTimeDiff >= 0 And $sEndTestDateTimeDiff >= 0 Then Return True Return False EndFuncI hope others find this UDF useful.In response to BrewManNH suggestion about date formats, here are some functions created by guinness that could be useful to use with this UDF in relation to US/UK date format: _UKToUSDate() and _USToUKDate()Lupo73 updated and added a function to this thread, _DateTimeStandard, to convert date and time to a format than can be used with this UDF.Edit: Removed extra @error values listed in _Time12HrTo24Hr header.Edit 2: Corrected header for _IsBetweenDatesTimesLite for $sEndDateTime.Edit 3: Added link to guinness' _UKToUSDate() and _USToUKDate() functions.Edit 4: Added link to Lupo73's _DateTimeStandard function.Edit 5: Forum update stripped out in regexes, added file for download.Adam Edited May 23, 2015 by AdamUL Updated broken links. PoojaKrishna, Gianni and RichardL 3 Link to comment Share on other sites More sharing options...
czardas Posted November 12, 2011 Share Posted November 12, 2011 It looks like there are some useful functions there. Thanks for sharing. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
mgmuck Posted November 29, 2011 Share Posted November 29, 2011 Thanks so much for posting the UDF-just what I needed for my project. Nicely done. Link to comment Share on other sites More sharing options...
AdamUL Posted November 29, 2011 Author Share Posted November 29, 2011 Thanks, I'm glad it was able to help you. Adam Link to comment Share on other sites More sharing options...
ozrug Posted December 17, 2011 Share Posted December 17, 2011 Adam: I'm sure developing this UDF took considerable time and effort on your part. Just the testing alone was probably an intensive and time-consuming task. Thank you for being kind enough to post it for others to use. It has exactly what I and mamy others need. Keep in mind that for every member who writes, there are many more who are appreciating your work even though they don't post a comment here. Thank you. Link to comment Share on other sites More sharing options...
AdamUL Posted December 19, 2011 Author Share Posted December 19, 2011 @ozrug I'm glad you was able to use this UDF. Thank you for the nice comments. Adam Link to comment Share on other sites More sharing options...
clicked Posted December 20, 2011 Share Posted December 20, 2011 Very nice. I have done some of these functions but yours look more nicely implemented, so I will probably end up using yours. Link to comment Share on other sites More sharing options...
BrewManNH Posted December 20, 2011 Share Posted December 20, 2011 _IsStandardDate format is only the correct standard in the US and a few other locations. There are several other formats that are standards in other countries, plus the ISO format should be considered "standard". If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
AdamUL Posted December 20, 2011 Author Share Posted December 20, 2011 @BrewManNHI wrote this UDF for a US based application with as simple functions with as simple understandable names as I could think of, and I thought I would share it. I also define what a "standard date" is in the function header for _IsStandardDate.Checks to see if a date is in a standard date format, "MM/DD/YYYY". I defined it as "a standard", not "the standard". The ISO "standard" date format is the "Calc" format in this UDF. Rename the functions however you need to, and change the RegEx to fit your needs for your defined "standard" that is needed. It is a simple RegEx. Looking at the UDF, it currently isn't compatible with a date in the "MM-DD-YYYY" format because it was not what I needed when I wrote it. If you would like additional functionality, please update a function or multiple functions, and post to this thread for others to use.@clickedThanks for the nice comments, and I glad you are able to use the UDF. Adam Link to comment Share on other sites More sharing options...
BrewManNH Posted December 20, 2011 Share Posted December 20, 2011 I personally have no need for the UDFs at this time, I just thought it prudent to point out that it's limited to US style date formats, in case you weren't aware of that. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
AdamUL Posted December 20, 2011 Author Share Posted December 20, 2011 I was aware of it, but thank you for pointing it out for others, that might not read the function headers. Who knows, I may update it in the future. I just thought it would be useful to others, that is why I posted it. Adam Link to comment Share on other sites More sharing options...
AdamUL Posted January 11, 2012 Author Share Posted January 11, 2012 Added a link to the first post to guinness' _UKToUSDate() and _USToUKDate() functions. Adam Link to comment Share on other sites More sharing options...
Lupo73 Posted March 2, 2012 Share Posted March 2, 2012 It could be interesting to improve this UDF adding support to convert between more date standards and to auto-detect system time format to perform the conversion. An idea could be to integrate this to load system time. It could be useful for example to convert image Date Taken (loaded using ) or for other uses. I'm going to try something in the next days. SFTPEx, AutoCompleteInput, _DateTimeStandard(), _ImageWriteResize(), _GUIGraduallyHide(): some AutoIt functions. Lupo PenSuite: all-in-one and completely free selection of portable programs and games. DropIt: a personal assistant to automatically manage your files. ArcThemALL!: application to multi-archive your files and folders. Link to comment Share on other sites More sharing options...
guinness Posted March 2, 2012 Share Posted March 2, 2012 Edit 3: Added link to guinness' _UKToUSDate() and _USToUKDate() functions.Sorry I'm a little late, thanks AdamUL! UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Lupo73 Posted March 5, 2012 Share Posted March 5, 2012 (edited) I improved and fixed the GreenCan function, that now can be used to convert a datetime string from system current user format or specified format to a standard format. expandcollapse popup; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateTimeStandard ; Description ...: Convert a date [and time] from system current user format or specified format to YYYY/MM/DD[ HH:MM:SS] ; Syntax ........: _DateTimeStandard($sInputDateTime[, $fWithSeparators = 1[, $sDateFormat = ""[, $sTimeFormat = ""]]]) ; Parameters ....: $sInputDateTime - Input date [and time]. ; $sDateFormat - Optional, Format of input date e.i. "MM-DD-YYYY". ; $sTimeFormat - Optional, Format of input time e.i. "HH.MM". ; Return values .: Success - Date with separators YYYY/MM/DD[ HH:MM:SS] or without separators YYYYMMDD[HHMMSS] ; Failure - "", sets @error to: ; |1 - Invalid date format. ; |2 - Invalid time format. ; Author ........: Sean Hart <autoit@hartmail.ca> ; Modified ......: Lupo73 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DateTimeStandard($sInputDateTime, $fWithSeparators = 1, $sDateFormat = "", $sTimeFormat = "") Local $sReturnString, $sInputDate, $sInputTime, $sYear, $sMonth, $sDay, $sHour, $sMin, $sSec Local $sDateSep, $sTimeSep, $sAM, $sPM, $isPM, $sTestDate, $aStringSplit1[9], $aStringSplit2[9] ; Read default system time formats and separators from registry if not provided If $sDateFormat = "" Then $sDateFormat = RegRead("HKEY_CURRENT_USERControl PanelInternational", "sShortDate") $sDateSep = RegRead("HKEY_CURRENT_USERControl PanelInternational", "sDate") Else For $A = 1 To StringLen($sDateFormat) If Not(StringMid($sDateFormat, $A, 1) = "y") And Not(StringMid($sDateFormat, $A, 1) = "m") And Not(StringMid($sDateFormat, $A, 1) = "d") Then $sDateSep = StringMid($sDateFormat, $A, 1) ExitLoop EndIf Next EndIf If $sTimeFormat = "" Then $sTimeFormat = RegRead("HKEY_CURRENT_USERControl PanelInternational", "sTimeFormat") $sTimeSep = RegRead("HKEY_CURRENT_USERControl PanelInternational", "sTime") $sAM = RegRead("HKEY_CURRENT_USERControl PanelInternational", "s1159") $sPM = RegRead("HKEY_CURRENT_USERControl PanelInternational", "s2359") Else For $A = 1 To StringLen($sTimeFormat) If Not(StringMid($sTimeFormat, $A, 1) = "h") Then $sTimeSep = StringMid($sTimeFormat, $A, 1) ExitLoop EndIf Next $sAM = "AM" $sPM = "PM" EndIf ; Separate date and time if included (make break at first space) If StringInStr($sInputDateTime, "T") Then $sInputDateTime = StringReplace($sInputDateTime, "T", " ") If StringInStr($sInputDateTime, " ") Then $sInputDate = StringLeft($sInputDateTime, StringInStr($sInputDateTime, " ") - 1) $sInputTime = StringStripWS(StringReplace($sInputDateTime, $sInputDate, ""), 7) Else $sInputDate = $sInputDateTime $sInputTime = "" EndIf ; Simple check of input date format (look for separators and unexpected non numeric characters) $sTestDate = StringReplace($sInputDate, $sDateSep, "") If StringRegExpReplace($sTestDate, "[0-9]", "") <> "" Then Return SetError(1, 0, "") If StringInStr($sInputDate, $sDateSep) = 0 And $sDateSep <> "" Then Return SetError(1, 0, "") If $sInputTime <> "" Then $sTestDate = StringReplace($sInputTime, $sTimeSep, "") $sTestDate = StringReplace($sTestDate, $sAM, "") $sTestDate = StringReplace($sTestDate, $sPM, "") $sTestDate = StringReplace($sTestDate, " ", "") If StringRegExpReplace($sTestDate, "[0-9]", "") <> "" Then Return SetError(2, 0, "") If StringInStr($sInputTime, $sTimeSep) = 0 And $sTimeSep <> "" Then Return SetError(2, 0, "") EndIf ; Break up date components (using format as a template), unless format is YYYYMMDD If $sDateFormat = "YYYYMMDD" Then $sYear = StringMid($sInputDate, 1, 4) $sMonth = StringMid($sInputDate, 5, 2) $sDay = StringMid($sInputDate, 7, 2) Else $aStringSplit1 = StringSplit($sDateFormat, $sDateSep) $aStringSplit2 = StringSplit($sInputDate, $sDateSep) For $A = 1 To $aStringSplit1[0] If StringInStr($aStringSplit1[$A], "m") Then $sMonth = $aStringSplit2[$A] If StringInStr($aStringSplit1[$A], "d") Then $sDay = $aStringSplit2[$A] If StringInStr($aStringSplit1[$A], "y") Then $sYear = $aStringSplit2[$A] Next EndIf ; Pad values with 0 if required and fix 2 digit year If StringLen($sMonth) = 1 Then $sMonth = "0" & $sMonth If StringLen($sDay) = 1 Then $sDay = "0" & $sDay If StringLen($sYear) = 2 Then If $sYear > 70 Then $sYear = "19" & $sYear Else $sYear = "20" & $sYear EndIf EndIf ; Break up time components (if given) If $sInputTime <> "" Then ; Look for AM/PM and note it, then remove from the string $isPM = 0 If StringInStr($sInputTime, $sAM) Then $sInputTime = StringReplace($sInputTime, " " & $sAM, "") $isPM = 1 ElseIf StringInStr($sInputTime, $sPM) Then $sInputTime = StringReplace($sInputTime, " " & $sPM, "") $isPM = 2 EndIf $aStringSplit1 = StringSplit($sTimeFormat, $sTimeSep) $aStringSplit2 = StringSplit($sInputTime, $sTimeSep) $sSec = "00" For $A = 1 To $aStringSplit2[0] If StringInStr($aStringSplit1[$A], "h") Then $sHour = $aStringSplit2[$A] If StringInStr($aStringSplit1[$A], "m") Then $sMin = $aStringSplit2[$A] If StringInStr($aStringSplit1[$A], "s") Then $sSec = $aStringSplit2[$A] Next ; Clean up time values (change hour to 24h and 0 pad values) If $isPM = 1 And $sHour = 12 Then $sHour = "00" If $isPM = 2 And $sHour < 12 Then $sHour = $sHour + 12 If StringLen($sHour) = 1 Then $sHour = "0" & $sHour If StringLen($sMin) = 1 Then $sMin = "0" & $sMin If StringLen($sSec) = 1 Then $sSec = "0" & $sSec $sInputTime = " " & $sHour & ":" & $sMin & ":" & $sSec EndIf $sReturnString = $sYear & "/" & $sMonth & "/" & $sDay & $sInputTime If $fWithSeparators = 0 Then $sReturnString = StringRegExpReplace($sReturnString, "[^0-9]", "") ; Remove all non-digit characters. EndIf Return $sReturnString EndFunc ;==>_DateTimeStandard Edited March 12, 2012 by Lupo73 SFTPEx, AutoCompleteInput, _DateTimeStandard(), _ImageWriteResize(), _GUIGraduallyHide(): some AutoIt functions. Lupo PenSuite: all-in-one and completely free selection of portable programs and games. DropIt: a personal assistant to automatically manage your files. ArcThemALL!: application to multi-archive your files and folders. Link to comment Share on other sites More sharing options...
AdamUL Posted March 5, 2012 Author Share Posted March 5, 2012 @guinness Your welcome. @Lupo73 Thanks for finding, updating, and posting a very useful function. I welcome your input for improving this UDF. I'm currently working on some other projects, and do not have time to update this UDF. Adam Link to comment Share on other sites More sharing options...
Lupo73 Posted March 12, 2012 Share Posted March 12, 2012 I made a minor update to _DateTimeStandard() function, adding support to output without separators. SFTPEx, AutoCompleteInput, _DateTimeStandard(), _ImageWriteResize(), _GUIGraduallyHide(): some AutoIt functions. Lupo PenSuite: all-in-one and completely free selection of portable programs and games. DropIt: a personal assistant to automatically manage your files. ArcThemALL!: application to multi-archive your files and folders. 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