Forums/Bugs & Fixes

Using date comparisons to make items go away at midnight

Michael Slater
posted this on June 28, 2010, 21:08

Many sites use data comparisons to make items go away when they are past. For example, suppose you have a custom item type "meeting" and it has a field "meeting_date", and you want to display only future meetings. You can do this with the following code:

<w:kb:item:each type="meeting" condition="meeting_date >= now">

    <h2><w:name />: <w:meeting_date /></h2>

</w:kb:item:each>

This code displays the name and date of all meetings whose meeting_date is today or later.

Until recently, such meetings would sometimes disappear up to a day before you'd expect, because dates had an invisible, and indeterminate, time associated with them.

Now, all dates have an implicit time of midnight, so the comparison will perform as expected.

Note, however, that "now" is the date and time in California (PDT or PST, depending on the season). So if the dates you are storing are in some other time zone, you'll need to adjust. For example, if you're on the east coast of the U.S., use the following:

<w:kb:item:each type="meeting" condition="meeting_date >= now + 3.hours">

If you're someplace that doesn't observe daylight savings time, and you need exact time comparisons, you'll need to change your code when the time change occurs.