Sign in to follow this  
Followers 0
Geert

UDF _StringToDate

1 post in this topic

Remake of TO_DATE Function for use in AutoIt.

Useful for contries with different date format. (like Holland)

Mask is supporting:

YYYY 4-digit year

MM Month (01-12; JAN = 01).

DD Day of month (1-31).

HH Hour of day (1-23).

MI Minute (0-59).

SS Second (0-59).

Feel free to optimize the code and extend the supported masks.

Geert

#include <Date.au3>
#include <Array.au3>

$CalculatableDate = _StringToDate("28-2-2006 16:22", "DD-MM-YYYY HH:MI")
If $CalculatableDate Then
    MsgBox(0, "$CalculatableDate is:", $CalculatableDate)
EndIf

; Calculated the days
MsgBox(4096, "", "Number of Days since 28-2-2006 16:22 : " & _DateDiff( 'd', _StringToDate("28-2-2006 16:22", "DD-MM-YYYY HH:MI"), _NowCalc()))


Func _StringToDate($sDate, $sDateFormatMask)
    Local $asDate, $asDateFormat, $asCalcDateFormat, $i
    $asDate = StringSplit(StringRegExpReplace($sDate, "[- :/.]", "|"), "|")
    $asDateFormat = StringSplit(StringRegExpReplace($sDateFormatMask, "[- :/.]", "|"), "|")
    $asCalcDateFormat = StringSplit("YYYY|MM|DD|HH|MI|SS", "|")
    For $i = 1 To $asCalcDateFormat[0]
        $asCalcDateFormat[$i] = _ArraySearch($asDateFormat, $asCalcDateFormat[$i], 1)
        If $asCalcDateFormat[$i] < 0 Then
            $asCalcDateFormat[$i] = "0"
        Else
            $asCalcDateFormat[$i] = $asDate[$asCalcDateFormat[$i]]
        EndIf
        If StringLen($asCalcDateFormat[$i]) < 2 Then $asCalcDateFormat[$i] = StringFormat("%02d", $asCalcDateFormat[$i])
    Next
    $sDate = $asCalcDateFormat[1] & "/" & $asCalcDateFormat[2] & "/" & $asCalcDateFormat[3] & " " & $asCalcDateFormat[4] & ":" & $asCalcDateFormat[5] & ":" & $asCalcDateFormat[6]
    If _DateIsValid($sDate) Then
        Return $sDate
    Else
        Return (0)
    EndIf
EndFunc   ;==>_StringToDate

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  
Followers 0