compare two dates

BlitzPlus Forums/BlitzPlus Programming/compare two dates

Sonari Eclipsi Onimari(Posted 2007) [#1]
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.


CS_TBL(Posted 2007) [#2]
If "1234"<"4321" Notify "yay" Else Notify "darn!"
End

So, make sure your date string is sorted as: "yyyy-mm-dd"


Sonari Eclipsi Onimari(Posted 2007) [#3]
Wait, its literally that simple?

if "2007-07-31" > "2007-07-30"
notify "Yup."
endif

Dang, thats easy, but is it reliable?


H&K(Posted 2007) [#4]
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)


plash(Posted 2007) [#5]
I doubt this kind of coding would last that many years :P


Yahfree(Posted 2007) [#6]
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


Dabz(Posted 2007) [#7]
If your using strings, I really recommend casting them to integers:-



Otherwise, the If expression returns 'nope', when infact, it should return 'yup'

Dabz


CS_TBL(Posted 2007) [#8]
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



Dabz(Posted 2007) [#9]
If Int("1234567890123456789") > Int("123456789012345678") Notify "yay" Else Notify "darn"
End


I would still cast it in the code, as thats my preference! :)

Dabz


H&K(Posted 2007) [#10]
I would still cast it in the code, as thats my preference!
And reintroduce the Bug. Why?

Int("1234567890123456789") = 2112454933


Dabz(Posted 2007) [#11]

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


JRalha(Posted 2007) [#12]
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...


Senzak(Posted 2007) [#13]
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