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);
Related
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);
}
When I run this code, the first date is shown in GMT, and the second in BST. Why is this? The calls to Date.UTC are identical apart from one changed digit, the month digit. That, to me, shouldn't warrant a change in timezone. Please note that I'm in London right now, so somehow the second date seems to be returning local time. Why is the timezone different for the two different dates?
var date1 = new Date(Date.UTC(2005,0,5,4,55,55));
alert(date1); // Wed Jan 05 2005 04:55:55 GMT+0000 (GMT)
var date2 = new Date(Date.UTC(2005,5,5,4,55,55)); // <-- 0 has been replaced by 5
alert(date2); // Sen Jun 05 2005 05:55:55 GMT+0100 (BST)
Using Date.UTC(), only effects setting the date using UTC.
As default Javascript dates display using localtime.
So if you wish to see the dates in UTC format,.
You can't just use the default toString() implementation, as that will use localtime version.
But what you can do is use the UTC variants for display. eg. toUTCString() and also toISOString().
var date2 = new Date(Date.UTC(2005,5,5,4,55,55));
//if you say live in the UK, this date in localtime is
//British Summer Time,..
//eg. Sun Jun 05 2005 05:55:55 GMT+0100 (GMT Summer Time)
//if your running this script from another country
//your likely to see something different.
console.log(date2.toString());
//here we show the date in UTC, it will be same
//whatever country your running this from
//eg. Sun, 05 Jun 2005 04:55:55 GMT
console.log(date2.toUTCString());
//for an easier to parse date,
//eg. 2005-06-05T04:55:55.000Z
console.log(date2.toISOString());
I get from data base this date string:
var MeasureDateStr = "2016-07-19T16:29:31";
On client I create datetime javascript object from MeasureDate value:
var measureDate = new Date(MeasureDateStr);
After the measureDate object is created the content is:
var measureDate = Tue Jul 19 2016 19:29:31 GMT+0300 (Jerusalem Daylight Time);
As you can see I have different time (+3 hours) relatively to original date string.
My question is why I get diffrent time in measureDate and how to fix the problem?
You get user local time because you don't have anything to tell browser in which timezone to look at.
If you want to have UTC time, just add Z at the end of time string:
var MeasureDateStr = "2016-07-19T16:29:31Z";
Also this may help:
How to ISO 8601 format a Date with Timezone Offset in JavaScript?
Are you sure?
The string you are reporting is a local time string.
If I run your example, I get a Tue Jul 19 2016 16:29:31 GMT+0200 (ora solare Europa occidentale), so the local time is preserved, while a time zone is attached.
If you add a Z, or a time zone indication (like 2016-07-19T16:29:31+03:00), you can specify exactly the behaviour you want.
Please note that when you log the date, you will always get a local format, even if the date was specified with another time zone:
var MeasureDateStr = "2016-07-19T16:29:31+03:00";
var measureDate = new Date(MeasureDateStr);
console.log(measureDate);
I get Tue Jul 19 2016 15:29:31 GMT+0200 (ora solare Europa occidentale) because I'm on GMT+2. But the time is converted accordingly.
It's because your browsers timezone is set to GMT+3. And since you do not specify a timezone in your datestring it will add 3 hours automatically.
EDIT:
var d = '2016-07-19T16:29:31';
var offset = new Date().getTimezoneOffset() * 60 * 1000; // get Timezone offset in milliseconds.
d = new Date(Date.parse(d) + offset) // Remove the timezone offset.
console.log(typeof d); // object
console.log(d); // Tue Jul 19 2016 16:29:31 GMT+0200 (CEST)
Old answer:
new Date('2016-07-19T16:29:31').toUTCString()
I have noticed a peculiar behavior when converting strings to Date objects in javascript. When doing this:
var date1 = new Date("2014-09-28T00:00:00");
console.log(date1);
The result is:
Sun Sep 28 2014 02:00:00 GMT+0200 (Srednja Europa - ljet. vrij.)
The same code with another date.
var date2 = new Date("2014-10-28T00:00:00");
console.log(date2);
gives this result:
Tue Oct 28 2014 01:00:00 GMT+0100 (Srednja Europa - st. vrij.)
Notice that the GMT offset in the first variable is 2 hours and in the second just 1 hour. The first one takes the summertime time calculation in account and the second one does not. Can anybody explain this? Have to mention that I am from Croatia where the current GMT offset is plus two hours and summertime is on.
According to timeanddate.com, Daylight Saving Time (DST) in Croatia ends on October 26th, 2014. Therefore, the first date (September 28th) includes it (GMT+2), and the second (October 28th) does not (GMT+1).
I just working on date comparison but face something strange with date. my code is something like below code
var now = new Date();
var from = new Date(2013,12,18,7,41,25,0);
var untill = new Date(2013,12,18,8,42,25,0);
if(now <= untill && now >= from){
...
}else{
....
}
this condition in a right time for example 2013/12/18 7:42:00 doesn't work.
I get further investigate about it with console.log :
console.log(from);
console.log(now);
console.log(untill);
the output was as strange as below:
Date {Sat Jan 18 2014 07:41:25 GMT+0330 (Iran Standard Time)}
Date {Wed Dec 18 2013 08:20:22 GMT+0330 (Iran Standard Time)}
Date {Sat Jan 18 2014 08:42:25 GMT+0330 (Iran Standard Time)}
As you can see it's a date for from and untill variable is 2014 January 18 in spite of entered 2013 / 12 / 18
Please help me to figure out why javascript act such this.
The month argument uses 0-based indexing.
Use var from = new Date(2013,11,18,7,41,25,0); for December.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
In Javascript months are 0-indexed, so January is 0, December is 11.
Change your code to
var from = new Date(2013,11,18,7,41,25,0);
var untill = new Date(2013,11,18,8,42,25,0);
You are getting this because the Month and the Day of the week These are based on 0-based indexing.
you can read about the Date in more detail here. Date in Javascript.