Is It Safe to Simply Compare Date Objects When Comparing Dates?

I’m asking because it seems you can compare a date object’s date fine, but assigning them to each other assigns the date objects date, in addition to the reference to the date object. So is it safe? Or should one always compare dates using .TotalSeconds?

I ran into this …

[code] while not rs.eof
lastUpdate_s = rs.idxField(1).stringValue

    if lastUpdate_s <> "" then
      lastUpdate_d.SqlDateTime = lastUpdate_s  // On the second iteration of the loop, this changes the date stored in d1 to lastUpdate_d date here. So the next line performs the compare using the wrong date.
      if lastUpdate_d < d1 then d1 = lastUpdate_d
    end if
    
    rs.moveNext
  wend[/code]

[code] while not rs.eof
lastUpdate_s = rs.idxField(1).stringValue

    if lastUpdate_s <> "" then
      lastUpdate_d.SqlDateTime = lastUpdate_s
      if lastUpdate_d < d1 then d1.SQLDateTime = lastUpdate_d.SQLDateTime  // If assign using .SQLDateTime, then on the second iteration of the loop, the above line won't change d1 to lastUpdate_d.
    end if
    
    rs.moveNext
  wend[/code]

Thank you

Comparing date objects works fine. But with lastUpdate_d.SqlDateTime = lastUpdate_s your comparing to strings, not date objects.

when you do something like

[code] dim d1 as new date
dim d2 as new date

if d1 = d2 then
// same date
end if
[/code]
what gets compared is the total seconds value
So you literally check to see IF thee represent the same point in time

HOWEVER, when you do

[code] dim d1 as new date
dim d2 as new date

d1 = d2
[/code]
This does NOT copy data from one to the other
What it does do is make it so you have 2 references to the same object

I edited my post because it was ridiculous.

Ok so basically, comparing is fine. When assigning, always use .TotalSeconds.

Thank you Norman.

Use the copy constructor.

d1 = new Date(d2)

What if you want to see if two dates are the same object? You can’t do this:

if d1=d2

…because it compares the Date.TotalSeconds values and not the object values.

Can you cast them to Object to compare?

if Object(d1)=Object(d2)

?

[quote=64867:@Eric Williams]What if you want to see if two dates are the same object? You can’t do this:

if d1=d2

…because it compares the Date.TotalSeconds values and not the object values.

Can you cast them to Object to compare?

if Object(d1)=Object(d2)

?[/quote]

You can use the ‘is’ comparison operator: http://documentation.xojo.com/index.php/Is