Although I have forthcoming posts that involve 2D drawing with dojox.gfx and rendering editable data with the really awesome dojox.grid enhancements that are landing in version 1.2 of the toolkit coming up later in the month, I just rediscovered a “note to self” (for about the tenth time) suggesting that I should write a quick blurb on the dojo.date module; right now seems like a good time as any to go ahead and check that box.
But manipulating JavaScript Date objects sounds *so* boring, right? Well, yea, it is kind of boring. And tedious. And a customer is probably not going to pay you *just* to write them some Date manipulation code, so it delivers little business value in and of itself. But that’s exactly why you want the toolkit’s help for this kind of task. In fact, this is exactly the kind of task any JavaScript utility worth its salt should be doing for you so that you can spend time on far more interesting things.
To acquaint you with the dojo.date module, let’s consider two commonly used functions it provides by way of a little routine that accepts an ISO 8601 formatted string that you might pull out of a database somewhere on the back end and returns a response of the form “5 mins ago”, “23 secs ago”, 1 “day ago”, etc. that tells you roughly how much time has passed since then — sort of like you see beside breaking news headlines. This example snippet works by first converting the input string into a JavaScript Date object with the dojo.date.stamp function and then calculating the time delta according to a unit of measurement, whether it be seconds, minutes, hours, or days with the help of the dojo.date.difference function.
Here, see for yourself:
<html>
<head>
<script type="text/javascript"
src="http://o.aolcdn.com/dojo/1.1/dojo/dojo.xd.js"
></script>
<script type="text/javascript">
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");
dojo.addOnLoad(function() {
howLongAgo = function(timestamp) {
var _timestamp = dojo.date.stamp.fromISOString(timestamp);
var _now = new Date();
var _secsAgo = dojo.date.difference(_timestamp, _now, "second");
var _minsAgo = dojo.date.difference(_timestamp, _now, "minute");
var _hoursAgo = dojo.date.difference(_timestamp, _now, "hour");
var _daysAgo = dojo.date.difference(_timestamp, _now, "day");
if (_daysAgo) {
var _howLongAgo = _daysAgo + " day";
}
else if (_hoursAgo) {
var _howLongAgo = _hoursAgo + " hour";
}
else if (_minsAgo) {
var _howLongAgo = _minsAgo + " min";
}
else if (_secsAgo) {
var _howLongAgo = _secsAgo + " sec";
}
else {
throw Error("unable to compute _howLongAgo ?!?");
}
return _howLongAgo.split(" ")[0] > 1 ? _howLongAgo+"s ago" : _howLongAgo+" ago";
}
dojo.byId("specialMessage").innerHTML = "<em>Dojo: The Definitive Guide was published "+howLongAgo("2008-06-17T00:00:00Z");
});
</script>
</head>
<body>
<div id="specialMessage"></div>
</body>
</html>
There are a few more goodies in the dojo.date module, including good support for formatting dates according to specific locales. Make the small investment in time now to save yourself from writing brittle code that you have to maintain yourself when these kinds of chores come up later.
And while it’s on your mind, be sure to pick up a copy of Dojo: The Definitive Guide for more battle-tested tips and techniques that can save you time.

