Dates are Evil! HELP!

L

labelle

I have an event listing on my website that is pulling from SQL.
Unfortunately, the listing is displaying all events, especially those
that are even 2 years old. I want to get rid of them and only display
events that are today or in the future. I need help!

Dim tmpEvents As Events
tmpEvents = New EventsMapper().GetEvents()
Dim tmpEvent As New [Event]
Dim tmpEventListing As String
For Each tmpEvent In tmpEvents
tmpEventListing += "<font style='font-family: Verdana;
font-size: 10px;'>" & tmpEvent.StartDate & "</font>" & " <br> " &
"<font style='font-family: Verdana; font-weight: bold; font-size:
10px;'>" & "<A href=javascript:blush:penpopup3(" & tmpEvent.ID.ToString &
")>" & tmpEvent.Title & "</a>" & "</font>" & "<br><br>"

If tmpEvent.StartDate <= Date.Now.ToShortDateString
Then
Label2.Text = tmpEventListing
Else
Label2.Enabled = False
End If
Next

This displays all the events, but does nothing for events in the past.
Can you help?
 
J

Jon

Wouldn't it be easier to simply change your sql to pull events with
"eventdate >= GETDATE()"
 
P

Patrice

Make sure you are actually working on "real" dates. Here you are comparing a
date (or perhaps text ?) with text.

For example here in France the 13 of January we'll have :

13/01/2006 < 20/01/1980 as text but of course 20/01/1980 < 13/01/2006 as a
date...
 
L

labelle

You'd think so, but no...because the same view is used to display
events on another table in the site that is accessed by users that
actually need to see past events to schedule future events.

I just figured it would be easy to put a "if the event date is equal to
or less than today, display it" addition to my code. Unfortunately
now, for some reason it does not seem that easy. Unless I am missing
something obvious?
 
L

labelle

The dates being posted in the database are being entered as 1/1/1900
and i know Date.Now.ToShortDateString gives a day that says # 3/3/2006
#

Are those hash signs messing me up? I just don't understand what's
going on. I set a breakpoint and it clearly shows tmpEvent.StartDate
as 1/1/2006 and Date.Now as today's date.
 
H

Homer J Simpson

If tmpEvent.StartDate <= Date.Now.ToShortDateString

This looks very wrong. You should compare the internal date representations
as this will prevent problems with different formats etc.
 
H

Homer J Simpson

The dates being posted in the database are being entered as 1/1/1900
and i know Date.Now.ToShortDateString gives a day that says # 3/3/2006#

Are those hash signs messing me up? I just don't understand what's
going on. I set a breakpoint and it clearly shows tmpEvent.StartDate
as 1/1/2006 and Date.Now as today's date.

It isn't the hash signs. Apples == Apples. Apples != Oranges. You are
comparing Apples to Oranges. What you see at a breakpoint is not what is
being compared. You should compare a date formatted DB field to Date.Now
 
A

Armin Zingler

If tmpEvent.StartDate <= Date.Now.ToShortDateString
Then


What's the data type of the StartDate property? If it' isn't String, you
should enable Option Strict.


Armin
 
C

Cor Ligthert [MVP]

Labelle,

As result from the answers from the others,

Try this one
\\\
If CDate(tmpEvent.StartDate) <= Date.Now 'This converts any right date in
your local setting to an internal date, which is in 100 nanoseconds ticsk
starting at ISO date 1-1-1 00:00:00
///
I hope this helps,

Cor
 
P

Patrice

Are you sure ? IMO the date is not enclosed in # but anyway
ToShortDateString returns a *string*. You shouldn't use this at all to
compare "real" dates and not strings :
If tmpEvent.StartDate <= Date.Now Then

Also keep in mind that the date is not "entered as 1/1/1900" in the database
(assuming you have used the appropriate data type). Dates are always stored
the same internally. How they are printed depends on regional settings but a
given date has always the same internal representation.

Just make sure to compare dates to dates (and not dates to strings) so that
you compare their intela representation and not how they "look like" as text
in a particular language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top