how can i compare 2 dates?

# Thread: how can i compare 2 dates?

1. Junior Member
Join Date
Dec 1969
Posts
5

## how can i compare 2 dates?

I have 3 dates stored in string format date1="10/8/2002", etc...<BR><BR>I just need to be able to ask the question,<BR>if ((date1 &#060;= date2) and (date3&#062;= date2)) then<BR> do something...<BR><BR>just comparing the variables like that doesn&#039;t seem to work. Any ideas?<BR>

2. Senior Member
Join Date
Dec 1969
Posts
2,493

## RE: how can i compare 2 dates?

using the split() and Cdate() functions<BR><BR>ArrayDates = split(String,",")<BR><BR>if Cdate(ArrayDates(0)) &#060;= Cdate(ArrayDates(1)) and Cdate(ArrayDates(2)) &#062;= Cdate(ArrayDates(1)) then<BR><BR><BR>

3. Junior Member
Join Date
Dec 1969
Posts
5

## RE: how can i compare 2 dates?

sweet man, thanks... CDate is what I couldn&#039;t remember. I appreciate it!

4. Senior Member
Join Date
Dec 1969
Posts
2,334

## why??

There are much simpler ways to do that.<BR><BR>Use the datediff function.<BR><BR>DateDiff("D", date1, date2)<BR><BR>Would be greater than 0 if date1 is greater than date2

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## How is that simpler?

Remember, the dates are starting out life as *strings*.<BR><BR>So you&#039;d have to do:<BR><BR>If DateDiff("d", CDate(strDate1), CDate(strDate2) ) &#062; 0 Then...<BR><BR>vs. the much simpler and more intuitive:<BR><BR>If CDate(strDate1) &#062; CDate(strDate2) Then<BR><BR>You don&#039;t get rid of the comparison operator when you use DateDiff and you don&#039;t get rid of the CDate calls, so how is it simpler?<BR><BR>[Okay, in actuality you probably *can* get rid of the CDATE calls. Because DateDiff will probably make them for you. But they will be there, anyway.]<BR><BR>Finally, using DateDiff("d",....) assumes that the two datestrings are on different days. What happens if they happen to be 50 milliseconds apart?<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
2,334

## Why use Split?

When DateDiff is there where you need to supply the two dates in order to find the different between two dates.<BR><BR>As to what if the two dates are on the same days,<BR><BR>We can squeeze the datediff down to seconds(miliseconds...Don&#039;t know).

7. Senior Member
Join Date
Dec 1969
Posts
96,118