Javascript Date creation difference - in IE vs chrome - javascript

I am getting a long from the backend system that represents a date.
I convert the long to javascript Date by using - new Date(long).
I get different dates for the same long in IE and Chrome.
The correct date is the one shown in Chrome.
example
For example:
new Date(1032382800000)
In IE the date value is - Wed Sep 18 2002 23:00:00 GMT +0200.
In Chrome date value is - Thu Sep 19 2002 00:00:00 GMT +0300.
How can I solve this discrepancy?

I have found a workaround.
Because the time is not relevant in those date objects, I run the following code:
var iTimezoneOffset = dDate.getTimezoneOffset() - (new Date()).getTimezoneOffset();
if (iTimezoneOffset != 0) {
dDate.setMinutes(dDate.getMinutes() + iTimezoneOffset);
}

Related

Javascript New Date Changing Hour Value

I am receiving times in the an AJAX request and am converting them using the new Date() function.
I receive 2013-06-18T12:00:15Z
However, somehow I get the following after new Date():
Tue Jun 18 2013 08:00:15 GMT-0400 (EDT)
Why is it not:
Tue Jun 18 2013 12:00
See the following demo:
http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_convert
This is a time zone problem. You must be in the EDT timezone (GMT-0400). To correctly parse the date you should tell the parser in which timezone your date is correct.
For you parse your date like this :
new Date('2013-06-18 12:00:15 GMT-0400')
"GMT-0400" means GMT time minus 4 hours
Or if you don't wish to reformat your string, you can use the date.getUTC* functions to get the time as you parsed it.
The full list is available at Mozilla's documentation.
I agree with Vaim Caen's answer that this is a timezone issue, but not with parsing - the date is being parsed fine, but into your local timezone, while you're expecting it to be parsed into UTC date.
This answer shows how to convert from your current timezone to UTC - applying this to the TryIt demo gives:
var msec = Date.parse("2013-06-18T12:00:15Z");
// or: var msec = Date.parse("Tue Jun 18 2013 08:00:15 GMT-0400 (EDT)");
var d = new Date(msec);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
document.getElementById("demo").innerHTML = d;
Edit: If you all you're interested in is displaying the date (no further manipulations) then you can use:
d.toUTCString()
which will show the date in GMT (for me it actually shows "GMT" so most likely not of use!)
The alternative is to add a function to the prototype to show the date in whatever format you want and use the date.getUTC* methods.

datetime in Javascript alert more 2 GMT hours

Working with datetime in Javascript(apache/php/js project) and making alert for debugging I get message like "Sun Nov 30 2014 02:00:00 GMT+0200 ",
though in var time was not specified at all. That is gvfery confusing...
Which is the best and safe way to get rid of this GMT 2 hours ?
Use .toISOString(), e.g:
var myDate = new Date().toISOString();
For older browsers, which don't natively support this method, the function definition can be found here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toISOString

new Date is formatting differently on Firefox and Chrome

I am passing this new Date into both Firefox and Chrome console (same computer, and time zone) and I am getting mixed results. Chrome is pushing the time forward to my time zone and Firefox is using the passed in time! So confusing...
Firefox
new Date("2014-02-27T17:00:00Z") // Passing in Console
// Result: Date 2014-02-27T17:00:00.000Z
Chrome / Safari
new Date("2014-02-27T17:00:00Z") // Passing in Console
// Result: Thu Feb 27 2014 18:00:00 GMT+0100 (CET)
It is 1 hour in the difference off. Chrome says the time is 18:00:00 while FF says the time is 17:00:00 which is what I expected seen as I formatted to Zulu (UTC) time.
Any help on how to get a consistent date on what is passed in to all browsers?
Thanks
As you can see, Chrome gives you a local time (GMT+0100). That why the hour part is different.
You can try converting to UTC string.
new Date("2014-02-27T17:00:00Z").toUTCString()
// both Chrome and Firefox will give you "Thu, 27 Feb 2014 17:00:00 GMT"

Setting multiple date variables - issue with vars being overridden

I program mostly backend Ruby code and am trying to do some front end JS work that i'm really not familiar with.
I'm basically trying to pre-fill a number of fields with international dates based on a master UK date. Each international date is determined with a simple addition or subtraction of a few days.
Here's a short version of what I have done. Line by line it works fine in chrome console but for some reason when setting the date on each country variable, they seem to be carried fwd and influence the next one. I don't understand what's happening as surely the independently named vars should be able to be altered independently? I've added the console.log output with a comment on each.
Any help would be much appreciated.
$('#gb_date').change(function() {
//Grab GB date
gb = new Date($('#gb_date').val());
console.log(gb) // Mon Mar 03 2014 00:00:00 GMT+0000 (GMT) : This is correct and as expected
// Initially set territory dates vars to equal the gb date
var ie = gb;
var de = gb;
// Then calculate and set territory dates by adding or subtracting days
ie.setDate(ie.getDate() - 3); //Friday before
console.log(ie); // Fri Feb 28 2014 00:00:00 GMT+0000 (GMT) : Again as expected
de.setDate(de.getDate() + 4); //Friday after
console.log(ie); // Tue Mar 04 2014 00:00:00 GMT+0000 : Why has ie been reset here??
console.log(de); // Tue Mar 04 2014 00:00:00 GMT+0000 : Why is this being set based off the ie value and not the de var set above??
});
});
This is happening because ie,de, and gb are all the same object so you are setting and getting from the same object. You need to make each one have their own separate Date object
//Create new Date objects based off the old one.
var ie = new Date(gb);
var de = new Date(gb);
ie.setDate(ie.getDate() - 3);
de.setDate(de.getDate() + 4);

Chrome does not return correct hour in javascript

First try is in IE 9 console:
new Date('2013-10-24T07:32:53')
Thu Oct 24 07:32:53 UTC+0200 2013
returns as expected
Next try is in FireFox 24 console:
new Date('2013-10-24T07:32:53')
Date {Thu Oct 24 2013 07:32:53 GMT+0200 (Central Europe Standard Time)}
Then I go into Chrome 30 console:
new Date('2013-10-24T07:32:53')
Thu Oct 24 2013 09:32:53 GMT+0200 (Central Europe Daylight Time)
But the time is 09 here, it should be 07.
Is this a bug in chrome or am I doing something wrong here?
I can't use any other format than this '2013-10-24T07:32:53' that I get by JSON from C#.
I need to get the hour of this timestamp, with the getHours I get the incorect value in Chrome.
Solution:
var inputHour = input.split('T')[1];
inputHour = inputHour.substring(0, 2);
Its no bug. The implementation of date parse function differs across browsers & so does the format of the dateString accepted by it.
However this format seems to work same across ... link:
new Date("October 13, 1975 11:13:00")
If possible, try and use
new Date(year, month, day, hours, minutes, seconds, milliseconds)
for guaranteed results.
Regarding your format try parsing it yourself. Something like :
var str = '2013-10-24T07:32:53'.split("T");
var date = str[0].split("-");
var time = str[1].split(":");
var myDate = new Date(date[0], date[1]-1, date[2], time[0], time[1], time[2], 0);
Note (Thanks to RobG for this) : The Date constructor used above expects month as 0 - 11 & since October is 10 as per date String, the month has to be modified before passing it to the constructor.
Reference.
See this thread:
Why does Date.parse give incorrect results?
It looks like the behavior of the parsing signature of the Date constructor is completely implementation dependent.
Given:
var s = '2013-10-24T07:32:53';
in ES5 compliant browsers you could do:
var d = new Date(s + 'Z');
but for compatibility across all browsers in use, better to use (assuming date is UTC):
function dateFromString(s) {
s = s.split(/\D/);
return new Date(Date.UTC(s[0],--s[1],s[2],s[3],s[4],s[5]));
}

Categories