Chrome and Firefox parse Date differently - javascript

I found chrome parse some date pattern differently, the time string "2015-07-13T07:30:00" got from moment(time).format()
Chrome:
new Date("2015-07-13T07:30:00").toISOString()
// output "2015-07-13T07:30:00.000Z"
Firefox:
new Date("2015-07-13T07:30:00").toISOString()
// output "2015-07-12T23:30:00.000Z"
Firefox's output is my desired output, Chrome automatically assumes the timezone is utf offset 0, how can I get fix this?

Related

How to fix parsing error by moment.js on Safari?

How to fix parsing error by moment.js?
Const.DATE_MONTH_FORMAT = 'DD-MMM-YYYY';
var fromDate = moment(new Date(period.fromDate)).format(Const.DATE_MONTH_FORMAT);
On Firefox, Chrome: 19-Aug-2016
On Safari: Invalid date
On Firefox:
console.debug(period);
Object { fromDate="19/Aug/2016", toDate="30/Aug/2016"}
format() is used for output, not for determining what format the input is in. You're also passing period.fromDate to a Date constructor, rather than to moment, so you're using the browser's own parsing, which can do whatever it likes when dates don't conform to the JS spec (which DD/MMM/YYYY doesn't). Firefox and Chrome evidently think they can pluck a date from period.fromDate, while Safari can't. You should be able to fix this by passing in the string directly to moment, along with the format of period.fromDate, so it can do all the work itself:
Const.DATE_MONTH_FORMAT = 'DD-MMM-YYYY';
var fromDate = moment(period.fromDate, "DD/MMM/YYYY").format(Const.DATE_MONTH_FORMAT);

Why is Date.prototype.getTime() giving different values for Chrome and Firefox?

I have the following code,
var x = new Date("2016-04-04T00:00:00").getTime();
console.log(x);
The output of x is 1459728000000 in Chrome and 1459753200000in Firefox.
But I want Chrome's output in Firefox too - is there a workaround?
Chrome defaults to UTC when you don't specify a timezone, while Firefox defaults to your system timezone. Specify that you mean UTC by appending a Z to your time:
var x = new Date("2016-04-04T00:00:00Z").getTime();

Firefox showing date as NaN

I ma using code like
$filter('date')(new Date(), $scope.format);
$scope.format = 'dd-MMM-yyyy';
It works properly in all browsers except firefox. Firefox show value as NaN.
How do I fix this issue?
if your code involves new Date('<dd-MMM-yyyy>') at some point,
I would like to point it out that Firefox doesn't take that format!
new Date('<dd/MMM/yyyy>') would be fine.
This may happen due differing implementations in browsers when parsing date strings. The browser will use the locale to parse the date string, and as locals are subject to change, the returning string may not match the parsing method.
You can avoid it by returning the date's time in milliseconds instead of the date's string (which happens when calling the date's empty constructor).
Replace new Date() with (new Date()).getTime(), and you should be fine.

getTime in chrome and firefox different result

I need to convert Date object to timestamp,so create new Date object from datetime and use getTime,but it makes different result in Chrome and Firefox.it depends on timezone.
var date = new Date('2013-08-26T14:30:00');
date.getTime();
//1377527400000 in Chrome
//1377511200000 in Firefox
date.getTimezoneOffset();
//-270 in both of them
Firefox attention to timezone ,but chrome don't care about it.How can I force Firefox to act like chrome in this situation?And Why they act different?
I'm searching for the way difference than following psudo code:
if (Firefox){
// plus with 270*60*1000
}
--
datetime returned from MySQL,then replace space by T in javascript.
Working Demo Here
try using the standard date/time format:
var date = new Date("mm dd, yy hh:mm:ss");
See your code on JSFiddle
try to use this format:
(new Date('2013-08-26T14:30:00.0Z')).getTime();
and you'l get 1377527400000 for both

Javascript "Invalid Date" error in Safari

I've been trying to debug a script of mine and I can't get my head around what's wrong with this:
var date = new Date("19871104071535".replace(
/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
'$4:$5:$6 $2/$3/$1'
));
alert(date);
It works in Firefox, IE, Chrome but Safari gives me an "Invalid Date" error. Any ideas?
The Time and Date are in the wrong order (for just Safari I guess :):
I tested this in Safari and it works (I just swapped Date and Time position in the final string):
var date = new Date("19871104071535".replace(
/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
'$2/$3/$1 $4:$5:$6'
));
alert(date);
It will also work in the other browsers because this is what is expected.

Categories