Search code examples
javascriptstringdateformatlocale

format datestring based on locale without creating Date object (JavaScript)


I have a date string which looks like this: 2013-04-06T14:15:00 I'm looking for functions similar to toLocaleDateString() (documentation). However, those functions don't take a String parameter; you need to create a Date object first. I'm trying to avoid timezones altogether, so does anyone know of a function (standard or from a plugin) which can format a datestring using a specific locale's rules (1/17/2013 vs 17/1/2013 etc.) using only my datestring?

I'm currently using jQuery, and this plugin for formatting dates: jQuery.dateFormat


Solution

  • Pass your date string to the Date constructor. It parses most legitimate formats.

    new Date("1/17/2013")
    new Date("2013-04-06T14:15:00")