Jump to content
Sign in to follow this  

UDF _StringToDate

Recommended Posts


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.


#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)

; 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"
            $asCalcDateFormat[$i] = $asDate[$asCalcDateFormat[$i]]
        If StringLen($asCalcDateFormat[$i]) < 2 Then $asCalcDateFormat[$i] = StringFormat("%02d", $asCalcDateFormat[$i])
    $sDate = $asCalcDateFormat[1] & "/" & $asCalcDateFormat[2] & "/" & $asCalcDateFormat[3] & " " & $asCalcDateFormat[4] & ":" & $asCalcDateFormat[5] & ":" & $asCalcDateFormat[6]
    If _DateIsValid($sDate) Then
        Return $sDate
        Return (0)
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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.