compare two dates
BlitzPlus Forums/BlitzPlus Programming/compare two dates
| ||
I know this is stupid, but is there an algorithim to determine if one date is less than the other? I mean like if one date is newer or older than the other? Thanks. |
| ||
If "1234"<"4321" Notify "yay" Else Notify "darn!" End So, make sure your date string is sorted as: "yyyy-mm-dd" |
| ||
Wait, its literally that simple? if "2007-07-31" > "2007-07-30" notify "Yup." endif Dang, thats easy, but is it reliable? |
| ||
Dang, thats easy, but is it reliable? Nope, cos the year 2147483649 would appear as -1 so it would say that the 21st June 2147483641 was before 21st June 2001 (Bit like the millenium bug)(This was a joke) |
| ||
I doubt this kind of coding would last that many years :P |
| ||
basicly, they have a limit on how many numbers it can store, but if you do this for example... [code] curMonth="02" curYear="2007" curDate="21" If curMonth < "03" and curYear < "2008" and curDate < "22" print "yup." else print "nope." end if |
| ||
If your using strings, I really recommend casting them to integers:- Otherwise, the If expression returns 'nope', when infact, it should return 'yup' Dabz |
| ||
Nah, just compare strings like "2007-03-29" with "2008-03-31". How simple can it be? No milleniumbugs either:If "1234567890123456789" > "123456789012345678" Notify "yay" Else Notify "darn" End |
| ||
If Int("1234567890123456789") > Int("123456789012345678") Notify "yay" Else Notify "darn" End I would still cast it in the code, as thats my preference! :) Dabz |
| ||
I would still cast it in the code, as thats my preference! And reintroduce the Bug. Why?Int("1234567890123456789") = 2112454933 |
| ||
And reintroduce the Bug. Why? Sorry, miles away (On the phone @ the same time as posting the above)... When doing the date thing, I would cast it:- Sorry if I mangled it, but regarding comparing dates in the format described in Yahfree's post, not large integers! :D Dabz |
| ||
The code does not work because if you had something like 23-04-2005 it would return "nope" since it failed two of the test conditions (day and month) - although the target date is lot earlier than the tested one... But I'm having a few doubts of the best method myself of doing this job... |
| ||
I think this should work, if this is what you were asking for.;uses mm-dd-yyyy format, passed as a string Function CompareDates(date1$,date2$) ;returns 1 if date1 is less, 2 if date2 is less, 0 if they are equal m1 = Int(Mid(date1$,1,2)) d1 = Int(Mid(date1$,4,2)) y1 = Int(Mid(date1$,7,4)) m2 = Int(Mid(date2$,1,2)) d2 = Int(Mid(date2$,4,2)) y2 = Int(Mid(date2$,7,4)) If y1 = y2 And m1 = m2 And d1 = d2 Then Return 0 If y1 < y2 Return 1 ElseIf y1 = y2 And m1 < m2 Return 1 ElseIf y1 = y2 And m1 = m2 And d1 < d2 Return 1 Else Return 2 End If End Function |