Jump to content

_Date_Time_Convert


Malkey
 Share

Recommended Posts

The aim of this _Date_Time_Convert() function is to make converting date/time values in one date/time format to another easy.

This _Date_Time_Convert() function uses the Date and Time Picker Control's "format strings" to convert a date and/or time from its existing format to any other custom format.

You need to know that y, M, d, H or h, m, s, tt, stands for year, month, day, hour, minute, second, AM/PM, respectively. And you need to be able to represent the existing format of the date/time input string using the appropriate format characters.

For example:-

If the first parameter, $sDateTime, of the _Date_Time_Convert() function is "Thursday, 1 April, 2010 @ 08:05:08 PM", then you need to create the format string, the second parameter, $sDateTimeFormat, "dddd, d MMMM, yyyy @ hh:mm:ss tt".

The third and last parameter is another format string style, $sRetFormat, specifying the format of the output/return string. The default return style is "yyyy/MM/dd HH:mm:ss" which is the format required for _DateAdd, _DateDiff, and other functions.

Please describe the bug if you find one.
 

Local $aEg[5][3] = [["10:19:2010 08:13:55", "MM:dd:yyyy HH:mm:ss", "yyyy/MM/dd hh:mm:ss tt"], _
        ["1:4:2010 14:05:55", "M:d:yyyy HH:mm:ss", "yy/MM/dd h:m:s tt"], _
        ["06:19:2010", "MM/dd/yyyy", "yy/MM/dd"], _
        ["04:13:55 PM", "hh:mm:ss tt", "HH:mm:ss"], _
        ["Thursday, 12 April, 2010 @ 00:05 AM", "dddd, d MMMM, yyyy @ HH:mm tt", "yyyy-MM-d h:mtt"]]

For $j = 0 To UBound($aEg) - 1
    ConsoleWrite("In" & @CRLF & $aEg[$j][0] & @CRLF & $aEg[$j][1] & @CRLF & _
            "Out" & @CRLF & _Date_Time_Convert($aEg[$j][0], $aEg[$j][1], $aEg[$j][2]) & @CRLF & _
            $aEg[$j][2] & @CRLF & "=====================" & @CRLF & @CRLF)
Next

Local $sTimeDateIn = "Thursday, 1 April, 2010 @ 08:05 PM"
Local $sFormatIn = "dddd, d MMMM, yyyy @ hh:mm tt"
ConsoleWrite("In" & @CRLF & $sTimeDateIn & @CRLF & $sFormatIn & @CRLF & _
        "Out" & @CRLF & _Date_Time_Convert($sTimeDateIn, $sFormatIn) & _
        @CRLF & "yyyy/MM/dd HH:mm:ss default" & @CRLF & "=====================" & @CRLF & @CRLF)


; #FUNCTION# ==================================================================================
; Name...........: _Date_Time_Convert
; Description ...: Converts a date and/or time from its existing format to any other custom format.
; Example Date time "Thursday, 1 April, 2010 @ 08:05:08 PM"
; Example's Format Eg : "dddd, d MMMM, yyyy @ hh:mm:ss tt"
; Year. : yyyy = 2010 ; yy = 10
; Month : MMMM = April; MMM = Apr; MM = 04; M = 4
; Day.. : dddd = Thursday; ddd = Thu; dd = 01; d = 1
; Hour. : HH = 20; H = 20 (1 digit minimum); hh = 08; h = 8 (Lowercase h's used with AM/PM time)
; Minute: mm = 05 (2 digit minimum); m = 5 (1 digit minimum)
; Second: ss = 08 (2 digit minimum); s = 8 (1 digit minimum)
; AM/PM : tt = AM or PM; t = A or P
; Ref: Format Strings @ <a href='http://msdn.microsoft.com/en-us/library/bb761726%28v=VS.85%29.aspx#dtp_format_chars' class='bbc_url' title='External link' rel='nofollow external'>http://msdn.microsoft.com/en-us/library/bb761726%28v=VS.85%29.aspx#dtp_format_chars</a>
; =============================================================================================

Func _Date_Time_Convert($sDateTime, $sDateTimeFormat, $sRetFormat = "yyyy/MM/dd HH:mm:ss")
    Local $Time, $iYear, $iMnth, $iDay, $iHour, $iMinute, $iSec, $aDTFormat, $aDTVal, $iYearLen
    Local $aMMM[13] = [12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

    $aDTFormat = StringRegExp($sDateTimeFormat, "yyyy|yy|MMMM|MMM|MM|M|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|tt|t|\d+|[[:punct:]]", 3)
    $aDTVal = StringRegExp($sDateTime, "[a-zA-Z]+|\d{1,4}|[[:punct:]]", 3)
    If UBound($aDTFormat) <> UBound($aDTVal) Then
        MsgBox(0, "ERROR", " Possibly need a delineator between the digit values for year, month, day, hour, minute, or second.", 5)
        Return
    EndIf
    For $i = 0 To UBound($aDTFormat) - 1
        Select
            Case $aDTFormat[$i] == "yy" Or $aDTFormat[$i] == "yyyy" ; y - Year
                $iYearLen = StringLen($aDTFormat[$i])
                If $iYearLen = 4 Then $iYear = $aDTVal[$i] & "/"
                If $iYearLen = 2 Then
                    If Number($aDTVal[$i]) < Number(StringRight(@YEAR, 2) + 20) Then
                        $iYear = "20" & $aDTVal[$i] & "/"
                    Else
                        $iYear = "19" & $aDTVal[$i] & "/"
                    EndIf
                EndIf

            Case $aDTFormat[$i] == "M" Or $aDTFormat[$i] == "MM" Or $aDTFormat[$i] == "MMM" Or $aDTFormat[$i] == "MMMM" ; M - Month
                If StringLen($aDTFormat[$i]) > 2 Then
                    For $j = 1 To UBound($aMMM) - 1
                        If StringLeft($aDTVal[$i], 3) = $aMMM[$j] Then $aDTVal[$i] = $j
                    Next
                EndIf
                $iMnth = StringRight("0" & $aDTVal[$i], 2) & "/"

            Case $aDTFormat[$i] == "d" Or $aDTFormat[$i] == "dd" ; d - Day
                $iDay = StringRight("0" & $aDTVal[$i], 2) & " "

            Case $aDTFormat[$i] == "h" Or $aDTFormat[$i] == "hh" ; or StringRegExp( $aDTFormat[$i],"(?i)hh?tt?");  h - Hour
                $iHour = $aDTVal[$i]
                If $iHour = 12 Then $iHour = 0
                For $k = 0 To UBound($aDTFormat) - 1
                    If ($aDTFormat[$k] == "t" Or $aDTFormat[$k] == "tt") And StringLeft($aDTVal[$k], 1) = "p" Then $iHour = Mod(12 + $iHour, 24)
                Next
                $iHour = StringRight("0" & $iHour, 2) & ":"

            Case $aDTFormat[$i] == "H" Or $aDTFormat[$i] == "HH" ;  H - Hour
                $iHour = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "m" Or $aDTFormat[$i] == "mm" ;or StringRegExp( $aDTFormat[$i],"(?i)mm?tt?") ; m - Minute
                $iMinute = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "s" Or $aDTFormat[$i] == "ss" ; or StringRegExp( $aDTFormat[$i],"(?i)ss?tt?") ; s - Second
                $iSec = StringRight("0" & $aDTVal[$i], 2) & ":"
        EndSelect
    Next

    ; Default values added to empty, unused variables for entry into the Date Control.
    If $iYear = "" Then $iYear = "1900/"
    If $iMnth = "" Then $iMnth = "01/"
    If $iDay = "" Then $iDay = "01 "
    If $iHour = "" Then $iHour = "00:"
    If $iMinute = "" Then $iMinute = "00:"
    If $iSec = "" Then $iSec = "00"
    $Time = $iYear & $iMnth & $iDay & $iHour & $iMinute & $iSec

    ;===== The following converts $Time to $sRetFormat format using Date Control ======
    ; $Time is now in this format "yyyy/MM/dd HH:mm:ss"
    Local $hGui = GUICreate("My GUI get date", 200, 200, 800, 200)
    Local $idDate = GUICtrlCreateDate($Time, 10, 10, 185, 20)
    GUICtrlSendMsg($idDate, 0x1032, 0, $sRetFormat)
    Local $sReturn = GUICtrlRead($idDate)
    GUIDelete($hGui)
    Return $sReturn
EndFunc   ;==>_Date_Time_Convert

For a simple swapping of input string date and time values without the need for converting alpha months to digits, or adjusting the hour for am/pm, then I would use StringRegExpReplace back-referencing.

; Example "yyyy/MM/dd HH:mm:ss" to "dd/MM/yyyy HH:mm:ss"
ConsoleWrite(StringRegExpReplace("2010/04/01 20:05:00", "(\d{4})/(\d{2})/(\d{2})(.+)", "${3}/${2}/${1}${4}") & @CRLF) ; Displays 01/04/2010 20:05:00

Edit: Re-write of _Date_Time_Convert function.  It's the only way I could get it to work.

Edited by Malkey
Link to comment
Share on other sites

This is an example of finding the time difference between two dates, using the _Date_Time_Convert() function in conjunction with _DateDiff().
 

#include <Date.au3>

Local $sStartDate = "1/7/2008" ; "" ;
Local $sStartTime = "11:30AM"
Local $sEndDate = "01/07/2008" ; "" ;
Local $sEndTime = "3:00PM"

$sStart = _Date_Time_Convert($sStartDate & $sStartTime, "d/M/yyyyhh:mmtt") ; "hh:mmtt") ;
ConsoleWrite("$sStart = " & $sStart & @CRLF)

$sEnd = _Date_Time_Convert($sEndDate & $sEndTime, "dd/MM/yyyyh:mmtt") ; "h:mmtt") ;
ConsoleWrite("$sEnd = " & $sEnd & @CRLF)
;MsgBox(0,"_DateDiff", $sStart & @LF & $sEnd & @LF & "Diff: " & _DateDiff ("h", $sStart, $sEnd))

$iDateCalcD = _DateDiff('D', $sStart, $sEnd)
$iDateCalch = _DateDiff('h', $sStart, $sEnd)
$iDateCalcm = _DateDiff('n', $sStart, $sEnd)

MsgBox(4096, "Result", "Time difference " & $iDateCalcD & " Days " & Int($iDateCalch - $iDateCalcD * 24) & " hr " & _
        Int($iDateCalcm - $iDateCalch * 60) & "min")

; #FUNCTION# ==================================================================================
; Name...........: _Date_Time_Convert
; Description ...: Converts a date and/or time from its existing format to any other custom format.
; Example Date time "Thursday, 1 April, 2010 @ 08:05:08 PM"
; Example's Format Eg : "dddd, d MMMM, yyyy @ hh:mm:ss tt"
; Year. : yyyy = 2010 ; yy = 10
; Month : MMMM = April; MMM = Apr; MM = 04; M = 4
; Day.. : dddd = Thursday; ddd = Thu; dd = 01; d = 1
; Hour. : HH = 20; H = 20 (1 digit minimum); hh = 08; h = 8 (Lowercase h's used with AM/PM time)
; Minute: mm = 05 (2 digit minimum); m = 5 (1 digit minimum)
; Second: ss = 08 (2 digit minimum); s = 8 (1 digit minimum)
; AM/PM : tt = AM or PM; t = A or P
; Ref: Format Strings @ <a href='http://msdn.microsoft.com/en-us/library/bb761726%28v=VS.85%29.aspx#dtp_format_chars' class='bbc_url' title='External link' rel='nofollow external'>http://msdn.microsoft.com/en-us/library/bb761726%28v=VS.85%29.aspx#dtp_format_chars</a>
; =============================================================================================

Func _Date_Time_Convert($sDateTime, $sDateTimeFormat, $sRetFormat = "yyyy/MM/dd HH:mm:ss")
    Local $Time, $iYear, $iMnth, $iDay, $iHour, $iMinute, $iSec, $aDTFormat, $aDTVal, $iYearLen
    Local $aMMM[13] = [12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

    $aDTFormat = StringRegExp($sDateTimeFormat, "yyyy|yy|MMMM|MMM|MM|M|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|tt|t|\d+|[[:punct:]]", 3)
    $aDTVal = StringRegExp($sDateTime, "[a-zA-Z]+|\d{1,4}|[[:punct:]]", 3)
    If UBound($aDTFormat) <> UBound($aDTVal) Then
        MsgBox(0, "ERROR", " Possibly need a delineator between the digit values for year, month, day, hour, minute, or second.", 5)
        Return
    EndIf
    For $i = 0 To UBound($aDTFormat) - 1
        Select
            Case $aDTFormat[$i] == "yy" Or $aDTFormat[$i] == "yyyy" ; y - Year
                $iYearLen = StringLen($aDTFormat[$i])
                If $iYearLen = 4 Then $iYear = $aDTVal[$i] & "/"
                If $iYearLen = 2 Then
                    If Number($aDTVal[$i]) < Number(StringRight(@YEAR, 2) + 20) Then
                        $iYear = "20" & $aDTVal[$i] & "/"
                    Else
                        $iYear = "19" & $aDTVal[$i] & "/"
                    EndIf
                EndIf

            Case $aDTFormat[$i] == "M" Or $aDTFormat[$i] == "MM" Or $aDTFormat[$i] == "MMM" Or $aDTFormat[$i] == "MMMM" ; M - Month
                If StringLen($aDTFormat[$i]) > 2 Then
                    For $j = 1 To UBound($aMMM) - 1
                        If StringLeft($aDTVal[$i], 3) = $aMMM[$j] Then $aDTVal[$i] = $j
                    Next
                EndIf
                $iMnth = StringRight("0" & $aDTVal[$i], 2) & "/"

            Case $aDTFormat[$i] == "d" Or $aDTFormat[$i] == "dd" ; d - Day
                $iDay = StringRight("0" & $aDTVal[$i], 2) & " "

            Case $aDTFormat[$i] == "h" Or $aDTFormat[$i] == "hh" ; or StringRegExp( $aDTFormat[$i],"(?i)hh?tt?");  h - Hour
                $iHour = $aDTVal[$i]
                If $iHour = 12 Then $iHour = 0
                For $k = 0 To UBound($aDTFormat) - 1
                    If ($aDTFormat[$k] == "t" Or $aDTFormat[$k] == "tt") And StringLeft($aDTVal[$k], 1) = "p" Then $iHour = Mod(12 + $iHour, 24)
                Next
                $iHour = StringRight("0" & $iHour, 2) & ":"

            Case $aDTFormat[$i] == "H" Or $aDTFormat[$i] == "HH" ;  H - Hour
                $iHour = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "m" Or $aDTFormat[$i] == "mm" ;or StringRegExp( $aDTFormat[$i],"(?i)mm?tt?") ; m - Minute
                $iMinute = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "s" Or $aDTFormat[$i] == "ss" ; or StringRegExp( $aDTFormat[$i],"(?i)ss?tt?") ; s - Second
                $iSec = StringRight("0" & $aDTVal[$i], 2) & ":"
        EndSelect
    Next

    ; Default values added to empty, unused variables for entry into the Date Control.
    If $iYear = "" Then $iYear = "1900/"
    If $iMnth = "" Then $iMnth = "01/"
    If $iDay = "" Then $iDay = "01 "
    If $iHour = "" Then $iHour = "00:"
    If $iMinute = "" Then $iMinute = "00:"
    If $iSec = "" Then $iSec = "00"
    $Time = $iYear & $iMnth & $iDay & $iHour & $iMinute & $iSec

    ;===== The following converts $Time to $sRetFormat format using Date Control ======
    ; $Time is now in this format "yyyy/MM/dd HH:mm:ss"
    Local $hGui = GUICreate("My GUI get date", 200, 200, 800, 200)
    Local $idDate = GUICtrlCreateDate($Time, 10, 10, 185, 20)
    GUICtrlSendMsg($idDate, 0x1032, 0, $sRetFormat)
    Local $sReturn = GUICtrlRead($idDate)
    GUIDelete($hGui)
    Return $sReturn
EndFunc   ;==>_Date_Time_Convert

Edit: Update _Date_Time_Convert function.

Edited by Malkey
Link to comment
Share on other sites

  • 1 year later...

Hey this really works..

i used it to convert...

20-mar-1980 04:12:13 AM

$dt = _Date_Time_Convert($dt, "dd-MMM-yy hh:mm:ss tt", "yyyy/MM/dd HH:mm:ss")

this should be part of the standard auto it library! Is there some other way in auto it?

Why has no on else commented on this topic - thanks you are a legend...

Edited by ozmike
Link to comment
Share on other sites

  • 1 year later...
  • 5 months later...

Hi Malkey,

This doesn't work if the regional setting for date is not in english. Seems like because when converting month, in english it is "Jan", "Feb", "Mar" while in other language it can be other.

Is it possible to patch this bug?

Mean while, I modify it like this to suit my need:

Below this line:

Local $aMMM[12] = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

I add this line:

Local $aMMM_mycountry[12] = ["jan", "feb", "mar", "apr", "mai", "jun", "jul", "aug", "sep", "oct", "nov", "des"]

And I change this line:

If StringLeft($iMnth, 3) = $aMMM[$j] Then $iMnth = StringRight("0" & $j + 1, 2) & "/"

to:

If StringLeft($iMnth, 3) = $aMMM[$j] OR StringLeft($iMnth, 3) = $aMMM_mycountry[$j] Then $iMnth = StringRight("0" & $j + 1, 2) & "/"

It works so far

Link to comment
Share on other sites

  • 3 months later...

Great function malkey, I actually needed the use of this today. My question for you, is in the last 3 years have you come up with a different approach to using 

GUICtrlCreateDate?

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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Great function malkey, I actually needed the use of this today. My question for you, is in the last 3 years have you come up with a different approach to using 

GUICtrlCreateDate?

No.

By using the GUICtrlCreateDate function, I liked the ability to be able to easily use the standard Microsoft DateTime format characters.

It works for me and haven't given any thought to alternate methods.

Link to comment
Share on other sites

OK thanks. I also understand that your sending the message $DTM_SETFORMATW.

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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 2 months later...

Hi Malkey,

I have been having issues getting your function to work.  If I take your example, and slighty change the start and end time, 

Local $sStartDate = "1/7/2008" ; "" ;
Local $sStartTime = "11:31AM"
Local $sEndDate = "01/07/2008" ; "" ;
Local $sEndTime = "3:11PM"

$sStart = _Date_Time_Convert($sStartDate & $sStartTime, "d/M/yyyyhh:mmtt") ; "hh:mmtt") ;
ConsoleWrite("$sStart = " & $sStart & @CRLF)

$sEnd = _Date_Time_Convert($sEndDate & $sEndTime, "dd/MM/yyyyh:mmtt") ; "h:mmtt") ;
ConsoleWrite("$sEnd = " & $sEnd & @CRLF)
;MsgBox(0,"_DateDiff", $sStart & @LF & $sEnd & @LF & "Diff: " & _DateDiff ("h", $sStart, $sEnd))

I would expect the following output:

$sStart = 2008/07/01 11:31:00

$sEnd = 2008/07/01 15:11:00

but I actually get 

$sStart = 2008/07/01 11:31:00

$sEnd = 2008/07/01 15:01:00

Note: I only changed the Start time and End time.  All other code is unchanged.

What I really want to do is convert the following 

e.g. 3:10:15PM to 15:10:15 (format: h:mm:sstt to HH:mm:ss)

Your help will be greatly appreciated.

Johnty

Link to comment
Share on other sites

@johnty
Thanks for the motivation to tackle the bug in the _Date_Time_Convert function. I have now corrected and updated my first and second posts.

The output now it as expected.

Local $sStartDate = "1/7/2008" ; "" ;
Local $sStartTime = "11:31AM"
Local $sEndDate = "01/07/2008" ; "" ;
Local $sEndTime = "3:11PM"

$sStart = _Date_Time_Convert($sStartDate & $sStartTime, "d/M/yyyyhh:mmtt") ; "hh:mmtt") ;
ConsoleWrite("$sStart = " & $sStart & @CRLF)

$sEnd = _Date_Time_Convert($sEndDate & $sEndTime, "dd/MM/yyyyh:mmtt") ; "h:mmtt") ;
ConsoleWrite("$sEnd = " & $sEnd & @CRLF)
;MsgBox(0,"_DateDiff", $sStart & @LF & $sEnd & @LF & "Diff: " & _DateDiff ("h", $sStart, $sEnd))


$sConvert = _Date_Time_Convert("3:10:15PM", "h:mm:sstt", "HH:mm:ss") ; "h:mmtt")  ; 3:10:15PM to 15:10:15 (format: h:mm:sstt to HH:mm:ss)
ConsoleWrite("Convert 3:10:15PM to HH:mm:ss = " & $sConvert & @LF)

#cs
Output:-
$sStart = 2008/07/01 11:31:00
$sEnd = 2008/07/01 15:11:00
Convert 3:10:15PM to HH:mm:ss = 15:10:15
#ce

; #FUNCTION# ==================================================================================
; Name...........: _Date_Time_Convert
; Description ...: Converts a date and/or time from its existing format to any other custom format.
; Example Date time "Thursday, 1 April, 2010 @ 08:05:08 PM"
; Example's Format Eg : "dddd, d MMMM, yyyy @ hh:mm:ss tt"
; Year. : yyyy = 2010 ; yy = 10
; Month : MMMM = April; MMM = Apr; MM = 04; M = 4
; Day.. : dddd = Thursday; ddd = Thu; dd = 01; d = 1
; Hour. : HH = 20; H = 20 (1 digit minimum); hh = 08; h = 8 (Lowercase h's used with AM/PM time)
; Minute: mm = 05 (2 digit minimum); m = 5 (1 digit minimum)
; Second: ss = 08 (2 digit minimum); s = 8 (1 digit minimum)
; AM/PM : tt = AM or PM; t = A or P
; Ref: Format Strings @ <a href='http://msdn.microsoft.com/en-us/library/bb761726%28v=VS.85%29.aspx#dtp_format_chars' class='bbc_url' title='External link' rel='nofollow external'>http://msdn.microsoft.com/en-us/library/bb761726%28v=VS.85%29.aspx#dtp_format_chars</a>
; =============================================================================================

Func _Date_Time_Convert($sDateTime, $sDateTimeFormat, $sRetFormat = "yyyy/MM/dd HH:mm:ss")
    Local $Time, $iYear, $iMnth, $iDay, $iHour, $iMinute, $iSec, $aDTFormat, $aDTVal, $iYearLen
    Local $aMMM[13] = [12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

    $aDTFormat = StringRegExp($sDateTimeFormat, "yyyy|yy|MMMM|MMM|MM|M|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|tt|t|\d+|[[:punct:]]", 3)
    $aDTVal = StringRegExp($sDateTime, "[a-zA-Z]+|\d{1,4}|[[:punct:]]", 3)
    If UBound($aDTFormat) <> UBound($aDTVal) Then
        MsgBox(0, "ERROR", " Possibly need a delineator between the digit values for year, month, day, hour, minute, or second.", 5)
        Return
    EndIf
    For $i = 0 To UBound($aDTFormat) - 1
        Select
            Case $aDTFormat[$i] == "yy" Or $aDTFormat[$i] == "yyyy" ; y - Year
                $iYearLen = StringLen($aDTFormat[$i])
                If $iYearLen = 4 Then $iYear = $aDTVal[$i] & "/"
                If $iYearLen = 2 Then
                    If Number($aDTVal[$i]) < Number(StringRight(@YEAR, 2) + 20) Then
                        $iYear = "20" & $aDTVal[$i] & "/"
                    Else
                        $iYear = "19" & $aDTVal[$i] & "/"
                    EndIf
                EndIf

            Case $aDTFormat[$i] == "M" Or $aDTFormat[$i] == "MM" Or $aDTFormat[$i] == "MMM" Or $aDTFormat[$i] == "MMMM" ; M - Month
                If StringLen($aDTFormat[$i]) > 2 Then
                    For $j = 1 To UBound($aMMM) - 1
                        If StringLeft($aDTVal[$i], 3) = $aMMM[$j] Then $aDTVal[$i] = $j
                    Next
                EndIf
                $iMnth = StringRight("0" & $aDTVal[$i], 2) & "/"

            Case $aDTFormat[$i] == "d" Or $aDTFormat[$i] == "dd" ; d - Day
                $iDay = StringRight("0" & $aDTVal[$i], 2) & " "

            Case $aDTFormat[$i] == "h" Or $aDTFormat[$i] == "hh" ; or StringRegExp( $aDTFormat[$i],"(?i)hh?tt?");  h - Hour
                $iHour = $aDTVal[$i]
                If $iHour = 12 Then $iHour = 0
                For $k = 0 To UBound($aDTFormat) - 1
                    If ($aDTFormat[$k] == "t" Or $aDTFormat[$k] == "tt") And StringLeft($aDTVal[$k], 1) = "p" Then $iHour = Mod(12 + $iHour, 24)
                Next
                $iHour = StringRight("0" & $iHour, 2) & ":"

            Case $aDTFormat[$i] == "H" Or $aDTFormat[$i] == "HH" ;  H - Hour
                $iHour = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "m" Or $aDTFormat[$i] == "mm" ;or StringRegExp( $aDTFormat[$i],"(?i)mm?tt?") ; m - Minute
                $iMinute = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "s" Or $aDTFormat[$i] == "ss" ; or StringRegExp( $aDTFormat[$i],"(?i)ss?tt?") ; s - Second
                $iSec = StringRight("0" & $aDTVal[$i], 2) & ":"
        EndSelect
    Next

    ; Default values added to empty, unused variables for entry into the Date Control.
    If $iYear = "" Then $iYear = "1900/"
    If $iMnth = "" Then $iMnth = "01/"
    If $iDay = "" Then $iDay = "01 "
    If $iHour = "" Then $iHour = "00:"
    If $iMinute = "" Then $iMinute = "00:"
    If $iSec = "" Then $iSec = "00"
    $Time = $iYear & $iMnth & $iDay & $iHour & $iMinute & $iSec

    ;===== The following converts $Time to $sRetFormat format using Date Control ======
    ; $Time is now in this format "yyyy/MM/dd HH:mm:ss"
    Local $hGui = GUICreate("My GUI get date", 200, 200, 800, 200)
    Local $idDate = GUICtrlCreateDate($Time, 10, 10, 185, 20)
    GUICtrlSendMsg($idDate, 0x1032, 0, $sRetFormat)
    Local $sReturn = GUICtrlRead($idDate)
    GUIDelete($hGui)
    Return $sReturn
EndFunc   ;==>_Date_Time_Convert
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...