Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stange javascript Date behaviour on particular dates [duplicate]

i noticed a strange behave of javascript on manging this particular Date

"31-12-2017 23:59:59"

i have an API which i call and returns me some date as String, and i have to cast them to Date using js.

My code is like that

while (i<array.length){
                                array[i].start_date = new Date (array[i].start_date);
                                array[i].start_date.setHours(array[i].start_date.getHours()+1);
                                array[i].start_date = $filter('date')(array[i].start_date, "dd-MM-yyyy HH:mm:ss");

                                array[i].end_date = new Date (array[i].end_date);
                                array[i].end_date.setHours(array[i].end_date.getHours()+1);
                                array[i].end_date = $filter('date')(array[i].end_date, "dd-MM-yyyy HH:mm:ss");  
                                $scope.insSupplies.push(array[i]);

                        i++;
                    };

where array is the API i'm questioning.

The problem here is that when my code tries this

array[i].end_date = new Date (array[i].end_date);

on this date

"31-12-2017 23:59:59"

array[i].end_date becomes Invalid Date

How that happens?

EDIT:

var date = "31-12-2017 23:59:59"
var dateSplitted = date.split("-");
var dateInverted = dateSplitted[1] +"-" +dateSplitted[0] +"-"+dateSplitted[2];
dateSplitted = dateInverted.split(" ");
alert(dateSplitted[0]+"T"+dateSplitted[1]+"Z");
alert(new Date(dateSplitted[0]+"T"+dateSplitted[1]+"Z"));

I implemented this rough method to parse my date, but i don't get how is wrong Any help?

like image 766
Anon Avatar asked Apr 21 '26 19:04

Anon


2 Answers

That date isn't in any format that the Date object is required to parse, nor a format that is commonly supported even though it isn't required.

The only format Date is required to parse is the ISO-8601 subset defined by the specification. That date, in that format, would be "2017-12-31T23:59:59".

For historical reasons, xx-xx-xxxx is commonly treated as a date in U.S. format (even in non-U.S. locales), which is MM-DD-YYYY rather than the more rational format common outside the U.S. of DD-MM-YYYY. But even that isn't at all universal, and again isn't specified. The only format that isn't specified but is, as far as I know, universal is DD/MM/YYYY (again U.S. format).

like image 66
2 revsT.J. Crowder Avatar answered Apr 23 '26 07:04

2 revsT.J. Crowder


I don't know what is the issue going on here. But the solution is your month and date format is wrongly placed to convert.

Your date format should be 12-31-2017 23:59:59 instead of 31-12-2017 23:59:59. like mm/dd/yyyy. may be it's works like your browser default date format.

you can check this below alert results.

alert(new Date("12-31-2017 23:59:59"))// this is a correct format.

alert(new Date("31-12-2017 23:59:59"))// here `31` is month. The default month count is 12. but here it's automatically add 19 months into 12 month(31-12). So it will go to future year.

alert(new Date("13-12-2017 23:59:59"))// here you can check the result of Jan month 2018 

EDIT:

You should swap the month and date before convert. try this below code

 

var date= "31-12-2017 23:59:59".split("-");

var newDate = date[1] +"-" +date[0] +"-" +date[2];

alert(new Date(newDate));
like image 43
Ramesh Rajendran Avatar answered Apr 23 '26 07:04

Ramesh Rajendran