Jump to content
Sign in to follow this  
Champak

_DateFormatConvert

Recommended Posts

Champak

I have quite a few places where I needed to change the date format YYYY/MM/DD to MM/DD/YYYY or something else, and I got tired of stringsplitting things all over the place, so I made this, and figured it may be the only function in my script that may be generally useful to other people. My first Example...I'm a big boy now :P .

#Include <Array.au3>
;YMD = YYYY/MM/DD
;MDY = MM/DD/YYYY
;DMY = DD/MM/YYYY
;MY = MM/YYYY
;DM = DD/MM

$DFC_Test = _DateFormatConvert("02/04/1980", "MDY", "YMD")
If @error = 1 Then 
    MsgBox(0,0,"Error")
Else
    MsgBox(0,"Date Example",$DFC_Test)
EndIf

Func _DateFormatConvert($s_DFC_Date, $s_DFC_InFormat = "YMD", $s_DFC_OutFormat = "MDY")

    $s_DFC_GoodFormat = False

    Local $a_DFC_DateConvertArray[6]
        $a_DFC_DateConvertArray[0] = "YMD"
        $a_DFC_DateConvertArray[1] = "MDY"
        $a_DFC_DateConvertArray[2] = "DMY"
        $a_DFC_DateConvertArray[3] = "MY";======Not working
        $a_DFC_DateConvertArray[4] = "DM";======Not working
        $a_DFC_DateConvertArray[5] = "MD"=======Not working

    ;============ERROR Check
    For $i = 0 To UBound($a_DFC_DateConvertArray) - 1
        If $a_DFC_DateConvertArray[$i] = $s_DFC_OutFormat Then
            $s_DFC_GoodFormat = True
        EndIf
    Next
    
    If $s_DFC_GoodFormat = False Then
        ConsoleWrite("!_DateFormatConvert = Incorrect input/output format")
        SetError(1)
        Return
    EndIf
    ;=======================

    $a_DFC_DateChange = StringSplit($s_DFC_Date, "/")
    $a_DFC_DateIn = StringSplit($s_DFC_InFormat, "")
    $a_DFC_DateOut = StringSplit($s_DFC_OutFormat, "")
    ;_ArrayDisplay($a_DFC_DateChange)
    Local $a_DFC_Output[UBound($a_DFC_DateOut) - 1]
    Local $a_DFC_Final[UBound($a_DFC_DateOut) - 1]
    For $i = 0 To UBound($a_DFC_DateConvertArray) - 1
        If $a_DFC_DateConvertArray[$i] = $s_DFC_InFormat Then
            $a_DFC_Output[0] = _ArraySearch($a_DFC_DateOut, $a_DFC_DateIn[1])
            _ArraySwap($a_DFC_Final[$a_DFC_Output[0]-1], $a_DFC_DateChange[1])
            $a_DFC_Output[1] = _ArraySearch($a_DFC_DateOut, $a_DFC_DateIn[2])
            _ArraySwap($a_DFC_Final[$a_DFC_Output[1]-1], $a_DFC_DateChange[2])
            $a_DFC_Output[2] = _ArraySearch($a_DFC_DateOut, $a_DFC_DateIn[3])
            _ArraySwap($a_DFC_Final[$a_DFC_Output[2]-1], $a_DFC_DateChange[3])
            ExitLoop
        EndIf
    Next
        
    ;_ArrayDisplay($a_DFC_Final)
    ;_ArrayDisplay($a_DFC_Output)
    Return StringReplace(_ArrayToString($a_DFC_Final), "|", "/")

EndFunc

I did this in about 15 min, and don't have time now to figure out how to get the "2 element" date format working (MD, DM, MY), so if anyone can pitch in.

NOTE: formats do not have to be MM or DD, they can just be M or D. It works either way.

Share this post


Link to post
Share on other sites
weaponx

I think you took something simple and complicated it.

;Convert YYYY/MM/DD to MM/DD/YYYY
$original = "1980/04/30"
$new = StringRegExpReplace($original, "\A(\d*)/(\d*)/(\d*)","$2/$3/$1")
MsgBox(0,"","Before: " & $original & @CRLF & "After: " & $new)

;Convert MM/DD/YYYY to YYYY/MM/DD
$original = "04/30/1980"
$new = StringRegExpReplace($original, "\A(\d*)/(\d*)/(\d*)","$3/$1/$2")
MsgBox(0,"","Before: " & $original & @CRLF & "After: " & $new)

Share this post


Link to post
Share on other sites
Champak

Complicated...Yes and no. More lines yes, but easier to understand and "universal". I, as well as quite a few others, barely understand stringregexpreplace. But I guess I/you could always put yours in a "standard" function to make it easier to understand and more universal...in accepting an input and spitting out what you want without switching up the "$1/$2/$3" order(I'll probably look into that when I have a minute). But I do like your version still, nice.

Share this post


Link to post
Share on other sites
DaRam

weaponx's solution is elegant and implements your example.

Perhaps if you needed to extend it, to provide further formatting options....

Here is my recycled code, warts and all (notice time formatting not implemented, neither is system date format awareness):

#include <Date.au3>
#include <GUIConstants.au3>

$Form1 = GUICreate("DateFormat Demo", 290, 112, 193, 115)
$Label1 = GUICtrlCreateLabel("Date Time Value:", 8, 8, 90, 17)
$Input1 = GUICtrlCreateInput(@MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC, 100, 8, 180, 21)
$Label2 = GUICtrlCreateLabel("Date Time Format:", 8, 36, 90, 17)
$Input2 = GUICtrlCreateInput("ddd dd MMMM, yyyy", 100, 33, 180, 21)
$Button1 = GUICtrlCreateButton("&Convert", 8, 64, 89, 25, 0)
$Input3 = GUICtrlCreateInput("", 100, 64, 180, 24)
GUISetState(@SW_SHOW)

GUICtrlSetData( $Input3, DateFormat( GUICtrlRead($Input1), GUICtrlRead($Input2) ) )

While 1
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
         ExitLoop
    Case $Button1
         GUICtrlSetData( $Input3, DateFormat( GUICtrlRead($Input1), GUICtrlRead($Input2) ) )
    EndSwitch
Wend

Func DateFormat($InputDate, $DateFmt = "mm/dd/yyyy")
Local $iPos, $DateValue = $InputDate, $TimeValue
  If StringLen($DateValue) < 6 Then Return "Invalid Date"
  $iPos = StringInStr($DateValue, " ") ; Find Space
  If $iPos > 0 Then
     $TimeValue = StringMid($InputDate, $iPos) ; Extract Time
     $DateValue = StringMid($DateValue, 1, $iPos - 1) ; Drop Time
  EndIf
  $DateValue = StringSplit($DateValue, "/")
  If @error Then Return "Invalid Date"
  If $DateValue[0] < 3 Then Return "Invalid Date"
  If Int(Number($DateValue[1])) < 1 Then Return "Invalid Month in Date"
  If Int(Number($DateValue[1])) > 12 Then Return "Invalid Month in Date"
  If Int(Number($DateValue[2])) < 1 Then Return "Invalid Day in Date"
  If Int(Number($DateValue[2])) > 31 Then Return "Invalid Day in Date"
  If Int(Number($DateValue[3])) < 0 Then Return "Invalid Year in Date"

  ;If Int(Number($DateValue[1])) < 10 Then $DateValue[1] = "0" & $DateValue[1]
  If Int(Number($DateValue[2])) < 10 Then $DateValue[2] = "0" & $DateValue[2]
  If Int(Number($DateValue[3])) < 10 Then $DateValue[3] = "0" & $DateValue[3]
  If Int(Number($DateValue[3])) < 100 Then $DateValue[3] = StringLeft(@YEAR,2) & $DateValue[3]

  $InputDate = $DateFmt
  $InputDate = StringReplace($InputDate, "d", "@") ; Convert All Day References to @
  $InputDate = StringReplace($InputDate, "m", "#") ; Convert All Month References to #
  $InputDate = StringReplace($InputDate, "y", "&") ; Convert All Year References to &
  $InputDate = StringReplace($InputDate, "&&&&", $DateValue[3])                       ; Century and Year
  $InputDate = StringReplace($InputDate, "&&", StringRight($DateValue[3],2))          ; Year Only
  $InputDate = StringReplace($InputDate, "&", "")          ; Discard leftover Year Indicator
  $InputDate = StringReplace($InputDate, "####", _DateMonthOfYear($DateValue[1], 0))  ; Long Month Name
  $InputDate = StringReplace($InputDate, "###", _DateMonthOfYear($DateValue[1], 1))   ; Short Month Name
  If StringLen($DateValue[1]) < 2 Then
     $InputDate = StringReplace($InputDate, "##", "0" & $DateValue[1])                ; Month Number 2 Digit
  Else
     $InputDate = StringReplace($InputDate, "##", $DateValue[1])                      ; Month Number 2 Digit
  EndIf
  $InputDate = StringReplace($InputDate, "#", int($DateValue[1]))                     ; Month Number
  $iPos = _DateToDayOfWeek($DateValue[3], $DateValue[1], $DateValue[2])               ; Day of Week Number
  $InputDate = StringReplace($InputDate, "@@@@", _DateDayOfWeek($iPos, 0))            ; Long Weekday Name
  $InputDate = StringReplace($InputDate, "@@@", _DateDayOfWeek($iPos, 1))             ; Short Weekday Name
  $InputDate = StringReplace($InputDate, "@@", $DateValue[2])                         ; Day Number 2 Digit
  $InputDate = StringReplace($InputDate, "@", int($DateValue[2]))                     ; Day Number
  ;If StringLen($TimeValue) > 1 Then
  ;   $InputDate = $InputDate & " " & $TimeValue
  ;EndIf
  Return $InputDate
EndFunc

Share this post


Link to post
Share on other sites
andybiochem

Heh! I did this a while back too! - I think it must be an AutoIT right-of-passage!

Func _Date_Convert($dDateIn, $nType)
;************************************************************
; Convert inputted date to different format
;************************************************************
; $dDate = inputted date to convert
; $nType = to/from AI/PC date style...
;   1 = from PC to AI (dd/mm/yyyy [hh:mm:ss] ---> yyyy/mm/dd [hh:mm:ss])
;   2 = from AI to PC (yyyy/mm/dd [hh:mm:ss] ---> dd/mm/yyyy [hh:mm:ss])
;   3 = from PC to Numbers (dd/mm/yyyy [hh:mm:ss] ---> yyyymmdd[hhmmss])
;   4 = from AI to numbers (yyyy/mm/dd [hh:mm:ss] ---> yyyymmdd[hhmmss])
;   5 = from NUMBERS to PC (yyyymmdd[hhmmss] ---> dd/mm/yyyy [hh:mm:ss])
;   6 = from NUMBERS to AI (yyyymmdd[hhmmss] ---> yyyy/mm/dd [hh:mm:ss])
    Local $bTimeExists = 1
    If StringLen($dDateIn) <= 10 Then $bTimeExists = 0
    Local $dDateOut
    Switch $nType
        Case 1
            $dDateOut = StringMid($dDateIn, 7, 4) & "/"
            $dDateOut &= StringMid($dDateIn, 4, 2) & "/"
            $dDateOut &= StringMid($dDateIn, 1, 2)
            If $bTimeExists = 1 Then $dDateOut &= StringMid($dDateIn, 11)
        Case 2
            $dDateOut = StringMid($dDateIn, 9, 2) & "/"
            $dDateOut &= StringMid($dDateIn, 6, 2) & "/"
            $dDateOut &= StringMid($dDateIn, 1, 4)
            If $bTimeExists = 1 Then $dDateOut &= StringMid($dDateIn, 11)
        Case 3
            $dDateOut = StringMid($dDateIn, 7, 4)
            $dDateOut &= StringMid($dDateIn, 4, 2)
            $dDateOut &= StringMid($dDateIn, 1, 2)
            If $bTimeExists = 1 Then $dDateOut &= StringReplace(StringMid($dDateIn, 12), ":", "")
        Case 4
            $dDateOut = StringReplace(StringMid($dDateIn, 1, 10), "/", "")
            If $bTimeExists = 1 Then $dDateOut &= StringReplace(StringMid($dDateIn, 12), ":", "")
        Case 5
            $dDateOut = $dDateIn
            If $bTimeExists = 1 Then
                $dDateOut = StringMid($dDateOut, 1, 12) & ":" & StringMid($dDateOut, 13)
                $dDateOut = StringMid($dDateOut, 1, 10) & ":" & StringMid($dDateOut, 11)
                $dDateOut = StringMid($dDateOut, 1, 8) & " " & StringMid($dDateOut, 9)
            EndIf
            $dDateOut = StringMid($dDateOut, 1, 6) & "/" & StringMid($dDateOut, 7)
            $dDateOut = StringMid($dDateOut, 1, 4) & "/" & StringMid($dDateOut, 5)
            $dDateOut = _Date_Convert($dDateOut, 2)
        Case 6
            $dDateOut = $dDateIn
            If $bTimeExists = 1 Then
                $dDateOut = StringMid($dDateOut, 1, 12) & ":" & StringMid($dDateOut, 13)
                $dDateOut = StringMid($dDateOut, 1, 10) & ":" & StringMid($dDateOut, 11)
                $dDateOut = StringMid($dDateOut, 1, 8) & " " & StringMid($dDateOut, 9)
            EndIf
            $dDateOut = StringMid($dDateOut, 1, 6) & "/" & StringMid($dDateOut, 7)
            $dDateOut = StringMid($dDateOut, 1, 4) & "/" & StringMid($dDateOut, 5)
        Case Else
            $dDateOut = $dDateIn
    EndSwitch
    Return $dDateOut
EndFunc   ;==>_Date_Convert

....NOTE:

AI = AutoIt style (yyyy/mm/dd hh:mm:ss)

PC = normal (UK) PC style (dd/mm/yyyy hh:mm:ss)

A-m-a-z-i-n-g example WeaponX TY!!! - I never thought of doing it that way!!! To be honest I also only use reg exp if I absolutely must :P

Must do better!


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

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  

×