Convert dd.mm.yyyy format to yyyy-mm-dd

You can do this pretty simply. Just split the european date into an array, reverse it, and then join it with dashes.

var euro_date = '30.01.2010';
euro_date = euro_date.split('.');
var us_date = euro_date.reverse().join('-');

Datejs can parse that. The code is at http://datejs.googlecode.com/files/date.js

EDIT: It is not safe to left date.js determine the format string automatically. I made the mistake of not testing with a day <= 12 (duh). You should use:

Date.parseExact('09.01.2010', 'd.M.yyyy').toString('yyyy-MM-dd');

or

Date.parseExact('09.01.2010', 'dd.MM.yyyy').toString('yyyy-MM-dd');

depending on whether you want to allow single digit days.


Datejs is a bit bloated if you only need to do this. You can use split() and concatenate the results:

var eu_date = '30.01.2010';
var parts = eu_date.split('.');
var us_date = parts[2]+'-'+parts[1]+'-'+parts[0];

For these kinds of conversions where no date logic is needed, it's usually smartest to just use string manipulation tools.

Tags:

Javascript