Comparison between to date picker date time not working

Hi All.

Been playing around with DateTimePicker again, and I can’t seem to get it to work.
I have two dateTimePicker items, and I want to compare between the two, so that you can tell if you have hit a certain date time… but it doesn’t seem to work. The only option that I can see is the .SelectedDate. I don’t have SecondsSince 1970 as an option. Even after reading the documentation, I can’t seem to get it to work.

Here is my code:

if DateTimePicker2.SelectedDate = DateTimePicker1.SelectedDate then
MessageBox (“You have hit the date time”)
Timer2.Enabled = FALSE
else

end if

Any ideas as to what I am doing wrong?

Regards

You’re asking if one object equals the other here - whereas you want to compare a property of the two DateTime objects, i.e. SecondsFrom1970

if  DateTimePicker2.SelectedDate.SecondsFrom1970 = DateTimePicker1.SelectedDate.SecondsFrom1970  then etc etc

It’s worth noting that the time would also have to be identical. If that’s a problem, you might try the SQLDate method and compare the strings for equality.

Hi All.

Thanks for the replies. Tim: That didn’t work. When I looked at the date / time seconds from 1970, neither changes. So they will never be equal and never fire the Messagebox. I have tried refreshing the date time, and still no go.

Any other ideas or perhaps I am using DateTimePicker incorrectly?

Regards

Is this a desktop project? web project?
Are you only asking for date, time or date/time?

Hi All.

First, thanks for all of the help. It is appreciated.

It turns out my logic was flawed. I didn’t really want a start date time picker, just the end time, as you would want to have a future event happen from now (at least in this instance, in MY world! Someone else might want something different, but :smiley: )

Anyway, this code works for what I want it to do.
Not fancy.
Not pretty.
But it works.

var d1, d2 as Double

d2 = DateTimePicker2.SelectedDate.SecondsFrom1970
d1 = DateTime.Now.SecondsFrom1970

if Floor(d1) >= Floor(d2) then
MessageBox (“Alarm here”)
else

end if

Once again, thank you for the patience and help.

Regards

1 Like

Hi Michael,

You might like to look at this tutorial, as you might find it helpful: https://youtu.be/cWE3NP76wlU

The DateInterval is quite useful for calculating differences between two different dates. :slight_smile: