DateTime Intervals Versus DateTimes

I had wondered why some websites displayed date intervals (i.e. “5 days ago”, “4 hours ago”) for user posts instead of printing actual dates and times. Doing some new website work recently caused a revelation about that choice and how it solves many problems all at once.

Displaying a real date and time opens up a can of worms about what to display and how to display it. Date and time is fairly trivial as long as all users of a system are located in the same place. Each user thus shares a common timezone and expectation of what a date and time should look like.

Clocks

What time is it?

Creating and maintaining a site for users across multiple timezones and borders is more problematic.  Should a date look like one of the following?

  • 12/5/2014
  • 5/12/2014
  • 2014/12/5
  • 2014-12-5
  • Dec. 5, 2014
  • On and on and on

Then there’s the problem of time.  What timezone should be stored and displayed?  The timezone of the user who submitted the content being viewed?  The timezone of the web server?  The timezone of the user viewing the content?

If you insist on displaying a proper local time to every user on your site will have to geolocate every user and then convert the stored time to their local representation.  The timezone is only part of the problem.  You also have formatting:

  • 1:45 PM
  • 13:45
  • 13:45:05

Using date intervals solves all of these problems.  All dates stored in the database are in UTC.  All displayed date and time references are expressed as the amount of time that has elapsed since that time.  It’s a simple date diff calculation between the current UTC time and the UTC time stored in the database.

The exact same display is shown to all users, anywhere on the planet.  No user or locale-specific computation must be done.

This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published.