Make locale specific date / time strings in JavaScript

November 23rd 2013

When you're working with visitors from around the world, you'll at some point have a need to display a date / time in their locale format.

Thankfully, JavaScript makes this incredibly easy. Using a standard Date object:

Text Snippet:
var formatDate = function(dateIn) {
    return dateIn.toLocaleString();
};

var dateObject = new Date();
console.log(formatDate(dateObject));
// would log something like: 23/11/2013 10:32:45

That's pretty cool. And, if a user is in the US, or Japan locale, then their date (should) be formatted as they want.

There we have it! An easy, locale independent way of generating date strings.