Jump to content

Proper Date


Alodar
 Share

Recommended Posts

I keep needing to do date calculations, and I keep having dates in nowhere near the format that AutoIt's integrated functions can deal with, so I finally had to write a function to format dates back into the proper format.  Thought I'd share.  Maybe someone will neaten it up, or show a shorter way, but this is how I did it.

To be fair, the error settings aren't done (because I always know whether it's going to be a failure beforehand or not), so I should fix that, but there it is..

 

Func _ProperDate($sDateTime, $sFormat, $sDelimiter = "/", $sYear = "20")

    ; #FUNCTION# ;===============================================================================
    ;
    ; Name...........: _ProperDate
    ; Description ...: Extracts file/folder from ZIP compressed file
    ; Syntax.........: _ProperDate($sDate, $sFormat, $sDelimiter = "/", $sYear = "20"
    ; Parameters ....: $sDate - Date string
    ;                  $sFormat - Format of String (MDY, MYD, YMD, YDM, DMY, DYM)
    ;                  $sDelimiter - Delimiter string ("/", ",", "%20", etc)
    ;                  $sYear - Determines whether the beginning points of the year should start with 19 or 20
    ; Return values .: Success  - Returns proper date format for calculations YYYY/MM/DD[ HH:MM:SS]
    ;                  Failure - Returns 0 sets @error:
    ;                  |1 - Bad Format
    ;                  |2 - Date is Invalid
    ; Author ........: dwright
    ; example........: _ProperDate("11/20/1980", "MDY")
    ;==========================================================================================

    Local $sDate = StringRegExpReplace($sDateTime, " \d{1,2}:\d{1,2}:\d{1,2} [A|P]M", "")
    Local $sTime = " " & StringStripWS(StringReplace($sDateTime, $sDate, ""), 3)
    Local $aDate = StringSplit($sDate, $sDelimiter, 1)
    Local $i

    For $i = 1 To 3
        If StringLen($aDate[$i]) = 1 Then
            $aDate[$i] = "0" & $aDate[$i]
        EndIf
    Next

    Switch $sFormat
        Case "MDY"
            If StringLen($aDate[3]) = 2 Then
                $aDate[3] = $sYear & $aDate[3]
            EndIf

            Return StringStripWS($aDate[3] & "/" & $aDate[1] & "/" & $aDate[2] & $sTime, 3)

        Case "MYD"
            If StringLen($aDate[2]) = 2 Then
                $aDate[2] = $sYear & $aDate[2]
            EndIf

            Return StringStripWS($aDate[2] & "/" & $aDate[1] & "/" & $aDate[3] & $sTime, 3)

        Case "YMD"
            If StringLen($aDate[1]) = 2 Then
                $aDate[1] = $sYear & $aDate[3]
            EndIf

            Return StringStripWS($aDate[1] & "/" & $aDate[2] & "/" & $aDate[3] & $sTime, 3)

        Case "YDM"
            If StringLen($aDate[1]) = 2 Then
                $aDate[1] = $sYear & $aDate[3]
            EndIf

            Return StringStripWS($aDate[1] & "/" & $aDate[3] & "/" & $aDate[2] & $sTime, 3)

        Case "DMY"
            If StringLen($aDate[3]) = 2 Then
                $aDate[3] = $sYear & $aDate[3]
            EndIf

            Return StringStripWS($aDate[3] & "/" & $aDate[2] & "/" & $aDate[1] & $sTime, 3)

        Case "DYM"
            If StringLen($aDate[2]) = 2 Then
                $aDate[2] = $sYear & $aDate[3]
            EndIf

            Return StringStripWS($aDate[2] & "/" & $aDate[3] & "/" & $aDate[1] & $sTime, 3)

    EndSwitch


EndFunc   ;==>_ProperDate

 

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...