novatek Posted November 18, 2006 Posted November 18, 2006 This function has: - 1 or more known bugs and - 1 or more unwanted behaviors #include <GUIConstants.au3> #include <Date.au3> $s = _DateFormatToCalc("29.2.2004") MsgBox(0,"Date in Calc Format",$s) MsgBox(0,"Days Away From Today ", _DateDiff('D', $s, _NowCalcDate())) Func _DateFormatToCalc($sDate) $sCalcFormat = "yyyy'/'MM'/'dd" $winHandle = GUICreate ( "DateFormat2Calc", 210, 70, 400, -200) $ctrlDate = GUICtrlCreateDate ("", 10, 10, 185, 20 ) GuiSetState() ControlSend($winHandle,"",$ctrlDate, $sDate) GuiCtrlSendMsg($ctrlDate, 0x1005, 0, $sCalcFormat) $sDateCalc = GUICtrlRead($ctrlDate) GUIDelete() return $sDateCalc EndFunc Bug: When it reformats the date from 29.2.2004 to Calc Format, it returns 2004/02/28. (The d day is lost). When it reformats the date from 31.2.2004 to Calc Format, it returns 2004/02/01. "GuiCtrlSetData()" instead "ControlSend()" would be better solution to handle error (e.g. wrong date), but it is not able to write date in other format than Calc format, even though GuiCtrlSendMsg($ctrlDate, 0x1005, 0, "dd.MM.yyyy") is sent before. Unwanted is here GUI itself. GUI is only taken to make conversion. GuiSetState(@SW_HIDE) would be fine solution, but conversion then doesn't work. Otherwise it would be useful function, isn't it?
novatek Posted November 18, 2006 Author Posted November 18, 2006 (edited) Ok, this one should work (no GUI here): expandcollapse popup#include <Date.au3> $d = "29.2.2004" ;test date $s = _DateFormat2Calc($d) if $s = "" then MsgBox(0,"Error", "Invalid date or format: " & $d & @CRLF & @CRLF & _ 'Valid format is: "29.2.2004" OR "29.02.2004 11:22:33"' & @CRLF & @CRLF & _ 'Leading Zeros are Valid "09.02.2004", but Spaces NOT " 9. 2.2004"' & @CRLF & @CRLF & _ 'Valid delimiters: "29.2.2004", "29/2/2004", "29-2-2004", "29_2_2004"') else MsgBox(0,"Date in Calc Format",$s) MsgBox(0,"Days Away From Today ", _DateDiff('D', $s, _NowCalcDate())) endif Func _DateFormat2Calc($sDate) ;~ Requires: #include <Date.au3> ;~ Valid inputs: ("29.2.2004") OR ("29.02.2004 11:22:33") ;~ : Leading Zeros are Valid ("09.02.2004"), but Spaces NOT (" 9. 2.2004") ;~ Valid delimiters: ("29.2.2004"), ("29/2/2004"), ("29-2-2004"), ("29_2_2004") ;~ Success: RETURNs date in Calc Format ;~ Failure: RETURNs empty string "" (date is invalid or format is invalid) $aSplitDate = stringsplit($sDate, " ") $sDate = $aSplitDate[1] $aSplit = StringSplit($sDate, "./-_") if @error then return "" ;delimiters not found" if $aSplit[0] <> 3 then return "" ;format not valid" for $i = 1 to 3 if not number($aSplit[$i]) then return "" ;format not short" next $sCalc = $aSplit[3] &"/"& $aSplit[2] &"/"& $aSplit[1] if not _DateIsValid($sCalc) then return "" ;date not valid" return $sCalc EndFunc;==> DateFormat2Calc() by novaTek Edited November 19, 2006 by novatek
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now