Jewtus Posted February 3, 2015 Share Posted February 3, 2015 I always struggle with date functions... This is what I'm trying to do. Based on a given date, check to see if a 2nd date is closer to the first date (but not after) than the first of the year of the first date. Example 1 Date1=5/5/2014 Date2=5/5/2013 Now in this case, 5/1/2014 is closer to the first of the year than date 2, so I would want to display 1/1/2014. Example 2 Date1=5/5/2014 Date2=2/5/2014 In this case, the date 2 is closer to the first number, so I would want to display 2/5/2014. This is what I have in my code so far, but it doesn't work the way I want it to. $Date1='5/5/2015' $Date2='2/5/2014' $D1Split=StringSplit($Date1,"/") $D2Split=StringSplit($Date2,"/") If $Date1 > "1/1/"&$D2Split[3] Then MsgBox(0,"Date1 bigger","1/1/"&$D1Split[3]) Else MsgBox(0,"Date2 bigger",$Date2) EndIf I feel like I'm missing an and in the if statement but I have been racking my brain trying to figure it out. I've even tried using datediff and trying to work with those numbers so it was easier for me to wrap my head around (I have trouble with dates and date calculations) and I still couldn't figure it out. Link to comment Share on other sites More sharing options...
SorryButImaNewbie Posted February 3, 2015 Share Posted February 3, 2015 I'm maybe very wrong here but could the root of the problem be that $date1 and $date2 are strings? So < > can't really do anything with it Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 3, 2015 Moderators Share Posted February 3, 2015 Jewtus,I would do it like this: expandcollapse popup#include <Date.au3> $sDate_1 = "2014/01/05" $sDate_2 = "2013/05/05" $sDate_Ret = _CheckDates($sDate_1, $sDate_2) If @error Then MsgBox($MB_SYSTEMMODAL, "Error", "Date 2 is after Date 1") Else MsgBox($MB_SYSTEMMODAL, "Return", $sDate_Ret) EndIf $sDate_1 = "2014/05/05" $sDate_2 = "2014/05/02" $sDate_Ret = _CheckDates($sDate_1, $sDate_2) If @error Then MsgBox($MB_SYSTEMMODAL, "Error", "Date 2 is after Date 1") Else MsgBox($MB_SYSTEMMODAL, "Return", $sDate_Ret) EndIf Func _CheckDates($sDate_1, $sDate_2) ; Derive first day of year $sDate_First = StringLeft($sDate_1, 5) & "01/01" ; Check date2 before date1 If _DateDiff("D", $sDate_1, $sDate_2) > 0 Then Return SetError(1, 0, "") Else ; Now get differences between dates and datefirst $iDiff_1 = _DateDiff("D", $sDate_First, $sDate_1) $iDiff_2 = _DateDiff("D", $sDate_2, $sDate_First) ; And return closest to datefirst If $iDiff_1 < $iDiff_2 Then Return $sDate_1 Else Return $sDate_2 EndIf EndIf EndFuncObviously you need to add some date formatting code if you want to use a different format for the input - you might want to look at my Date_Time_Convert UDF which makes it very easy to do. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
SorryButImaNewbie Posted February 3, 2015 Share Posted February 3, 2015 So if a string consist only of numbers we can still use > and < ? Or is it because you used this _DateDiff function? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 3, 2015 Moderators Share Posted February 3, 2015 SorryButImaNewbie,Why not look in the Help file at what is returned from the _DateDiff function before asking questions like that? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
SorryButImaNewbie Posted February 3, 2015 Share Posted February 3, 2015 (edited) M23 I did that of course, i growth a bit in checking before asking thanks to you guys, it said "Returns the difference between 2 dates, expressed in the type requested" but i dont see where one can give that type, I mean okey i can tell that give me it back in seconds or days or what everelse. But does it give it back in string format ? edit: groth to growth edit2: I seems to fail to recall what is the name of the number format in a variable... (like string, boolean and aaaaa Edit3: its integer... for god's sake...) it made my search for it quite hard, but found in autohotkey that if a variable only contains numbers it automaticly take it as a number, but in autoit we can do it like: $var = "2" and $var = 2, so im not sure here, keep worming the help file Edited February 3, 2015 by SorryButImaNewbie Link to comment Share on other sites More sharing options...
Jewtus Posted February 3, 2015 Author Share Posted February 3, 2015 M23 Thanks, that pointed me in the right direction. I had actually already worked with the datediff, but the if conditional you laid out helped me figure out what I was doing wrong (I didn't have the datediff in the correct order which was giving me negative numbers). This is the function that did what I needed: Func _CheckDates($sDate_1, $sDate_2) If StringRight($sDate_1,4) > StringRight($sDate_2,4) Then Return '01/01/'&StringRight($sDate_1,4) ElseIf _DateDiff("D", StringRight($sDate_1,4)&'/'&StringLeft($sDate_1,6), StringRight($sDate_2,4)&'/'&StringLeft($sDate_2,6)) > 0 Then Return SetError(1, 0, "") Else $sDate_First = "01/01/"&StringRight($sDate_1, 4) ; Now get differences between dates and datefirst $iDiff_1 = _DateDiff("D", $sDate_First, $sDate_1) $iDiff_2 = _DateDiff("D", $sDate_2, $sDate_First) ; And return closest to datefirst If $iDiff_1 < $iDiff_2 Then Return "01/01"&StringLeft($sDate_1, 5) Else Return $sDate_2 EndIf EndIf EndFunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 3, 2015 Moderators Share Posted February 3, 2015 SorryButImaNewbie,The answer is there - try looking at this page. Basically $var = "2" stores the assigned value as a string, while $var = 2 stores it as a number (integer in this case). _DateDiff returns a number - it is the type of period (month, day, second, etc) required that you set. But the number is not an integer - as you can see here: #include <Date.au3> $sDate_1 = "2014/01/05" $sDate_2 = "2013/05/05" $iDiff = _DateDiff("D", $sDate_2, $sDate_1) MsgBox($MB_SYSTEMMODAL, "Return", $iDiff & @CRLF & VarGetType($iDiff))Clearer now? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
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