I need to convert 04/06/13 (for example) to a long date - Tue Jun 04 2013 00:00:00 GMT+0100 (BST). How can I do this using Javascript? I know how to convert a long date to a short date - just not the other way round.
-
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toISOString – techfoobar May 18 '13 at 17:42
4 Answers
You could try the parsing functionality of the Date
constructor, whose result you then can stringify:
> new Date("04/06/13").toString()
"Sun Apr 06 1913 00:00:00 GMT+0200" // or something
But the parsing is implementation-dependent, and there won't be many engines that interpret your odd DD/MM/YY
format correctly. If you had used MM/DD/YYYY
, it probably would be recognized everywhere.
Instead, you want to ensure how it is parsed, so have to do it yourself and feed the single parts into the constructor:
var parts = "04/06/13".split("/"),
date = new Date(+parts[2]+2000, parts[1]-1, +parts[0]);
console.log(date.toString()); // Tue Jun 04 2013 00:00:00 GMT+0200

- 630,263
- 148
- 957
- 1,375
-
That's strange, because on Chrome v25 "new Date("04/06/13")" evaluates to "Sat Apr 06 2013 00:00:00 GMT+0200 (CEST)" – Xotic750 May 18 '13 at 18:19
-
Why is that strange? Date parsing is implementation-dependent by spec, it only needs to recognize ISO8601 and its own `toString` output. I would even have *expected* an odd format like `DD/MM/YY` not to be parsed correctly everywhere. – Bergi May 18 '13 at 18:23
-
I thought that all implementations defaulted to 1900. Hence it was strange or a shock for me to see that this was not the case. – Xotic750 May 18 '13 at 18:26
-
1*don't* use the parsing functionality - it's locale dependent! Always use the latter form where you pass the year, month and date as numbers. – Alnitak May 18 '13 at 19:03
-
@Alnitak: Yeah, seems I didn't emphazise this enough. Answer updated :-) – Bergi May 18 '13 at 20:52
-
Works perfectly, thanks! How can I convert this long date back to a short date now? – James Anderson May 19 '13 at 09:28
-
You will need to manually construct it again (or use a [library](http://stackoverflow.com/a/12632212/1048572)). `Date` objects have useful methods, look into them! – Bergi May 19 '13 at 09:34
-
I've had a look around and can't find anything that will work. I'm trying to use http://momentjs.com for example, and I can't seem to be able to convert the long date to a short one? – James Anderson May 19 '13 at 09:49
-
2@Bergi NB: `dd/mm/yy` is _not_ an odd format - it's used by most of the world and it's a damn sight more logical than the US `mm/dd/yy` format! – Alnitak May 19 '13 at 11:10
-
@Alnitak: `dd/mm/yyyy` is OK. Yet the confusion with the really odd US format does make it not well-suited for exchanging dates - especially since browsers are not consistent there (and seem to favour the US format). Better would be the international standard `yyyy-mm-dd`. – Bergi May 19 '13 at 20:00
-
An alternative to the split method, uses lastIndexof and slice instead, to change the year to ISO8601 format which then gives a non-standard string that is known to be working across browsers, and then uses the date parsing method. (assuming a fixed pattern like in question)
However,
If you want to ensure how it is parsed, you have to do it yourself and feed the single parts into the constructor:
, this would mean using the split method, see @Bergi answer.
var string = "04/06/13",
index = string.lastIndexOf("/") + 1,
date = new Date(string.slice(0, index) + (2000 + parseInt(string.slice(index), 10)));
console.log(date);
Output
Sat Apr 06 2013 00:00:00 GMT+0200 (CEST)
On jsfiddle
or a further alternative would be to use moments.js
var string = "04/06/13";
console.log(moment(string, "DD/MM/YY").toString());
Output
Sat Apr 06 2013 00:00:00 GMT+0200 (CEST)
On jsfiddle
-
1`"04/06/2013"` is not [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601) - though the format is known to be working across browsers. – Bergi May 18 '13 at 18:50
-
1+1 for Moment.js. Most reliable way to do this consistently across browsers that I know of. – Matt Johnson-Pint May 19 '13 at 03:29
You can use:
new Date(2013, 06, 04)
...or directly using a date string (i.e. a string representing a date as accepted by the parse method):
new Date("2013/06/04");
...or by specifying the different parts of your date, like:
new Date(year, month, day [, hour, minute, second, millisecond]);
Take a look at this.

- 471
- 1
- 7
- 25
You have to take 13 as 2013 otherwise it will take default 1913
alert(new Date('04/06/2013'));

- 505
- 3
- 9