Why doesn't firefox break the date?

please help break the date line on components.

https://jsfiddle.net/xwvrtLt1/2/

in chrome it worked (console display date no problem). but in FF it does not work (console display errors)

JS:

var checkNeedZero = function(num) { 
    if(num < 10) { num = '0' + num; };
    return num;
};             


var dateRaw = '2016-03-14 17:37:12';

var date = new Date(dateRaw),
    monthNum = checkNeedZero(date.getMonth()),
    day = checkNeedZero(date.getDate()),
    year = date.getFullYear(),
    hours = checkNeedZero(date.getHours()),
    minutes = checkNeedZero(date.getMinutes());

console.log(date);

var vv = new Date(dateRaw);
console.log(vv);
console.log('---'); 
+4
source share
2 answers

The date format is not valid, but two different browsers treat it differently (Chrome works with an invalid date, while Firefox causes an error).

To make it valid, you can add a character Tbetween the date and time:

var dateRaw = '2016-03-14T17:37:12';

but I get the date in a specific format. and i can't change it

In this case, you need to add the character Tusing JavaScript:

var dateRaw = '2016-03-14 17:37:12'.replace(' ', 'T');

() T.

+3

Date , Date.parse(string), , , NaN.

: https://msdn.microsoft.com/en-us/library/ff743760(v=vs.94).aspx#Anchor_2 , , ISO8601 (YYYY-MM-DDTHH: mm: ss.sssZ - Internet Explorer 9 , quirks), : https://msdn.microsoft.com/en-us/library/ff743760(v=vs.94).aspx#Anchor_4

ECMA script 6.0 ISO8601: http://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format, , , .

: , "T", .

+3

All Articles