Formatting Dates in Javascript

November 01, 2011

As all Javascript Developers know, to work with dates in Javascript you would use the Date object. No surprise there. But what if you want to display a date in your digital signage open source Gadget or Presentation in a particular format, say, November 1st, 2011? Although the Date object has numerous methods for manipulating dates, it is rather limited when it comes to formatting those dates.

Get started with Rise Vision

I've heard it said that the best Developer is a lazy Developer. Taking this advice to heart, I set out to see if somebody else had already built something that I could use. Happily, I discovered Datejs. Datejs is an open source Javascript library that makes it super simple to not only format dates, but to parse and process them as well. For example, to show today's date as November 1st, 2011, all we have to do is add one line of code:

Date.today().toString("MMMM dS, yyyy")

Datejs can do other cool things like determine if a given year is a leap year, check whether Daylight Saving Time is observed in the current time zone, and add seconds, minutes, hours or days to a given date.

You can find the documentation for Datejs here. I'm sure you will find this library quite handy the next time you find yourself having to do advanced date processing or formatting in your Gadgets or Presentations.