New Date() Is Working in Chrome But Not Firefox

new Date() is working in Chrome but not Firefox

You can't instantiate a date object any way you want. It has to be in a specific way. Here are some valid examples:

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

or

d1 = new Date("October 13, 1975 11:13:00")
d2 = new Date(79,5,24)
d3 = new Date(79,5,24,11,33,0)

Chrome must just be more flexible.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

From apsillers comment:

the EMCAScript specification requires exactly one date format (i.e., YYYY-MM-DDTHH:mm:ss.sssZ) but custom date formats may be freely supported by an implementation: "If the String does not conform to that [ECMAScript-defined] format the function may fall back to any implementation-specific heuristics or implementation-specific date formats." Chrome and FF simply have different "implementation-specific date formats."

new Date() works in Chrome but throws 'Invalid date' in Firefox

See the documentation for the Date constructor:

A string value representing a date, specified in a format recognized by the Date.parse() method. (These formats are IETF-compliant RFC 2822 timestamps, and also strings in a version of ISO8601.)

Since you aren't passing one of those formats, you are dependent on implementations having non-standard support for your format.

Chrome does. Firefox doesn't.

Use a date parsing library that lets you specify the format (such as date-fns/parse) or change the API so it outputs dates in the standard format.

new Date is not working on Firefox

You dont need to replace the T. It works without it (tested in Chrome and Firefox).

After setting the Date object, get it into UTC.

Working snippet below:

var myDate = new Date("2014-11-18T08:06:39.06");
// now set it to UTCvar myDateinUTC = Date.UTC(myDate.getFullYear(), myDate.getMonth(), myDate.getDate(), myDate.getHours(), myDate.getMinutes(), myDate.getSeconds(), myDate.getMilliseconds());
console.dir(myDateinUTC);
var myNewDate = new Date(myDateinUTC);
console.log(myNewDate.getMonth()); // just to test

new Date() works differently in Chrome and Firefox

The correct format for UTC would be 2013-02-27T17:00:00Z (Z is for Zulu Time). Append Z if not present to get correct UTC datetime string.

javascript new date working in chrome but not in firefox

This happens because your date is neither valid RFC2822 nor valid ISO 8601, which are the formats supported by dateString constructor in Date object. For some reason Chrome seems to accept a wider range of formats, including yours, which is not strictly valid ISO 8601 date. Valid date would be:

2014-06-02T00:00:00

The simplest fix I can think of is to replace the space with a T before feeding it to Date constructor:

var futureDate = new Date($(".camp_end_date").val().replace(" ", "T"));

new Date().toLocaleString not working on firefox

According to the MDN specification of Date, "dateString" can be either IETF-compliant RFC 2822 timestamps or a version of ISO8601. Your date string is neither of it. I'm not even sure what "107111" in the end of your string should be, so how should a computer figure that out?

In general, it is always advisable to use date strings in the format "YYYY-MM-DDTHH:mm:ss.sssZ".

new Date() shows differents results in Chrome or Firefox

This is just the behavior of the debug console. The two date values you showed are both the same, and are the correct value. You're just seeing the local time in Chrome, while Firefox chooses to show the UTC time in the debug console.

More accurately, Chrome, IE, and most other browsers simply call .toString() on the object, while Firefox is calling .toISOString().

FF Screenshot

Note that Firefox has a bug that us showing the wrong name of the time zone (Standard instead of Daylight), but you can see the debugger value matches the ISO8601 UTC value.



Related Topics



Leave a reply



Submit