I have problem with convert date which I get from API. Format is for example "16/09/25"
I try do it like this
var x = new Date(dateFromApi)
and console thorw me error.

- 135
- 2
- 18
-
Can you not send year as `YYYY` from API? – Rajesh Mar 01 '17 at 12:09
-
*"Format is for example "16/09/25"*: is your API not consistent in its date format? What other formats does it produce? – trincot Mar 01 '17 at 12:11
3 Answers
Parsing a date string is very simple. A function that will work in any host from IE 4 onward is:
function parseDMY(s) {
var b = s.split(/\D/);
return new Date(b[2], b[1]-1, b[0]);
}
console.log(parseDMY('16/09/25'));
Where the year is >= 0 or <= 99, 1900 is added so 25
becomes 1925
. Preserving years in this range (so 25 is 0025) requires an additional line of code.

- 142,382
- 31
- 172
- 209
Your format is DD/MM/YY
and it is not accepted by Date
and will throw an error.
This is because, as mentioned by @MattJohnson, the accepted Date formats vary by locale and the only official format is YYYY-MM-DD
(which is derived from ISO date string. Read here).
In most cases, Date
will accept the format YY-MM-DD
. So we can simply do this:
var date = "16/09/25"; // date received from API
var split_date = date.split('/'); // outputs ["16","09",""25"]
var rearranged_date = [split_date[1], split_date[0], split_date[2]].join('/'); // outputs "09/16/25"
var proper_date = new Date(rearranged_date);
In other cases, it is best to provide the full-year YYYY
instead of just YY
.

- 2,666
- 1
- 13
- 19
-
Actually, no. The input format is not `MM/DD/YY` everywhere. It varies by locale of the user. The only input format required by the spec is `YYYY-MM-DD`, which will also be interpreted as UTC. Basically, string parsing of dates is implementation dependent, and locale sensitive. – Matt Johnson-Pint Mar 01 '17 at 22:50
-
It is safest to provide the Date
constructor with the individual parts of the date (i.e., year, month and day of the month).
In ES6 you can provide those elements like this:
var x = new Date(...dateFromApi.split('/').reverse().map( (p,i) => p-(i%2) ));
The map
is needed to subtract one from the month number, as it should be zero-based in numeric format.
Note the new Date(year, month, day)
version of the constructor will assume 19xx when you provide only 2 digits.
var dateFromApi = "16/09/25"
var x = new Date(...dateFromApi.split('/').reverse().map( (p,i) => p-(i%2) ));
console.log(x.toDateString());
In ES5, it would be a bit longer, like this:
new (Date.bind.apply(Date, (dateFromApi+'/').split('/').reverse()
.map(function (p,i) { return p-(i==2); })));
var dateFromApi = "16/09/25"
var x = new (Date.bind.apply(Date, (dateFromApi+'/').split('/').reverse()
.map(function (p,i) { return p-(i==2); })));
console.log(x.toDateString());
Of course, this assumes that the input format is consistently in the order DD/MM/YY (or D/MM/YYYY, as long as the order is the same); that valid dates are passed, and that you accept how 2-digit years are mapped to 4-digit years.

- 317,000
- 35
- 244
- 286
-
There are 2 problems: 1. Month should be Sept, not Oct. 2. Year is coming 1925, I think it should be 2025 (I assume) – Soubhik Mondal Mar 01 '17 at 12:43
-
1Dates are quite tricky in JavaScript make sure the date created by new Date() is same as old (I mean in same timezone) if you have time information with incoming date in the API. If time is not something you need to worry it's good to go. – Vipin Dubey Mar 01 '17 at 13:08
-
@BlazeSahlzen, thanks for highlighting the 1st problem. That is fixed now. For the second, I had already put a disclaimer at the end of my answer. People should any way refrain from using 2-digit year notation. – trincot Mar 01 '17 at 13:27
-
But it's strange that it's giving `1925`. In my solution it's giving `2025`. Sure, it's not something else? – Soubhik Mondal Mar 01 '17 at 13:36
-
1With `new Date(year, month, day)` JavaScript will assume 19xx when you provide only 2 digits for the year part. – trincot Mar 01 '17 at 13:54
-
1It may be an intellectual challenge to write code using as many new features as possible, however it does not seem sensible in that it restricts the hosts in which it will run, is difficult to maintain, runs more slowly than simpler code and is more code (about twice as much) to write. – RobG Mar 01 '17 at 22:43
-
2@RobG, it is why my answer has an ES5 version. Anyway, date manipulation is poor in JavaScript; a library like `momentjs` offers most of what date manipulation tasks call for. – trincot Mar 01 '17 at 22:49