I got this strange JavaScript bug that I can seem to work arround or fix.
I am using some code to make 2 JavaScript dates, to insert events into a calendar component.
The dates are built the following way:
var endDate = new Date();
var startDate = new Date();
startDate.setDate(startDateDay);
startDate.setMonth(startDateMonth);
startDate.setFullYear(startDateYear);
startDate.setHours(2, 0, 0, 0);
endDate.setDate(endDateDay);
endDate.setMonth(endDateMonth);
endDate.setFullYear(endDateYear);
endDate.setHours(2, 0, 0, 0);
So, the dates are built using integers. These integers are determined by input, and using the debugger I can see 100% positive they are coming in correctly.
Now, ill describe 3 walkthroughs, 2 where it goes correctly and 1 where it goes wrong.
Using the following input:
endDateDay = 20
endDateMonth = 9
endDateYear = 2014
Gives the following date object as result:
Tue Oct 20 2014 02:00:00 GMT+0200 (W. Europe Daylight Time)
Using this input:
endDateDay = 13
endDateMonth = 9
endDateYear = 2014
Gives the following date object as result:
Tue Oct 13 2014 02:00:00 GMT+0200 (W. Europe Daylight Time)
Now, using this input:
endDateDay = 27
endDateMonth = 9
endDateYear = 2014
Gives the following date object as result:
Mon Oct 27 2014 02:00:00 **GMT+0100** (W. Europe Standard Time)
As you can see, for some strange reason the TimeZone is off. This gives errors in my application, and I need to find a way to get it fixed. Though, I cannot find any solution to it, let alone understand why it is actually happening.
PS: I am using Google Chrome
The answer was indeed the difference in the daylight savings time, which I completly oversaw. Thanks to finding this out I also found a solution to my problem.
I used this link to further assist me, might it help someone in the future:
http://javascript.about.com/library/bldst.htm
Cheers!
Related
I have a project where if the end date is set for February 12 (can be set to any future date), the following is obtained from the API response.
project: {endDateTime:"1518393600000"}
For UTC time and date, this response corresponds to Mon Feb 12 2018 00:00:00
For local time and date, the response corresponds to Sun Feb 11 2018 19:00:00 (GMT - 05:00)
On the UI, I need to show the end date as Feb 12, 2018, but the date is getting converted to the local date and time zone and shows Feb 11 as the end date. My code is below:
var d = new Date();
var c = d.setTime(parseInt($scope.project.endDateTime));
$scope.endDateTime = c;
In the html
<div> {{endDateTime}} </div>
I tried modifying the code in the following way but it did not work.
var d = new Date($scope.project.endDateTime);
var c = d.getUTCDate();
$scope.endDateTime = c;
I tried to tune the code in other ways but could not get it to work. I know similar questions have been asked before but still could not get it to work, even after spending several hours. Maybe I am missing something very trivial. Some help would be greatly appreciated. :)
I have figured out the solution. My code and explanation is as follows
var d = new Date(parseInt($scope.project.endDateTime));
var c = c.toUTCString();
var endDate= c.split(" ");
$scope.endDateTime = endDate[2] + ' ' + endDate[1] + ',' + ' ' + endDate[3];
And the html is
<div> {{endDateTime}}</div>
I needed to parse the string that came back from the API response. Missed this part and also the correct method is toUTCString(), not getUTCDate().
So basically what happens is if the end date is set to Feb 12(in this case, it is dynamic though), the API returns
project: {endDateTime:"1518393600000"}
The first line of the code parses the string and if I do a console.log(d) I get this: Sun Feb 11 2018 19:00:00 GMT-0500 (Eastern Standard Time).
Now I need to display the time in GMT so the toUTCString() method was used in the second line and doing console.log(c) I get: Mon, 12 Feb 2018 00:00:00 GMT
The requirement is to show the date as Feb 12, 2018 on the UI, so I split the string in the third line and if i do console.log(endDate) I get
["Mon,", "12", "Feb", "2018", "00:00:00", "GMT"]
From the output of the third line, it was pretty easy to show the required date format. Hope this helps.
I wish to produce a date 30 days in the past using momentJS - it's pretty easy, I just use the following
const date30DaysPast = moment().utc().subtract(30, 'days').toDate(); // Sun Oct 29 2017 13:23:46 GMT+0100 (CET)
This is all great however I want the returned date to have no Time Zone or offset, I want the time to be GMT+0000 not as above GMT+0100 (CET) - for example:
Sun Oct 29 2017 13:23:46 GMT+0000
I wish to force this as I am using testing servers that are in different locations, and rather than take the time from the local browser I just want set a standard time. I thought using the utc method would do this for example should I write something like this in my test:
const oct4th2017 = moment.utc(new Date('October 04, 2017 11:13:00'));
the output is
Wed Oct 04 2017 09:13:00 GMT+0000
How can I remove the offset / time zone and set it to GMT on my original subtract method? I have tried wrapping in a parent utc method like so
const date30DaysPast = moment.utc(moment().utc().subtract(30, 'days').toDate());
but this doesn't work. I get the momentJS object.
Any advice could be appreciated, should my wording be bad or confusing please say so and I shall reword my question.
You already have your moment.js object without timezone. But the toDate() method creates a native Date object, which always has the local timezone (though it has various UTC methods).
This question already has answers here:
Javascript date parsing bug - fails for dates in June (??)
(5 answers)
Closed 8 years ago.
I'm getting some really strange behaviour with Javascript date objects.
var t = new Date();
t.setUTCFullYear(2014);
t.setUTCMonth(10);
t.setUTCDate(20);
console.log(t);
This returns:
Date {Sat Dec 20 2014 10:26:23 GMT-0800 (PST)}
Whereas if you use t.setMonth(10), you correctly get November. Am I doing something wrong?
This will work as expected tomorrow.
Today (October 31st), t is initialized with its day as 31. When you set the month to November, you're setting "November 31st", which is corrected to December 1st.
Then you set the date to 20, but the month has already changed. Set the date first, or better yet, use the two-argument verson of setUTCMonth():
t.setUTCMonth(10, 20);
Maybe I'm missing something (EDIT: I was, Paul's answer covers it), but you're right. However, this works:
var t = new Date();
t.setUTCFullYear(2014);
t.setUTCMonth(10, 20); // Set day here instead
console.log(t); // Thu Nov 20 2014 12:40:42 GMT-0600 (Central Standard Time)
It also works if you switch the order of the set statements:
var t = new Date();
t.setUTCFullYear(2014);
t.setUTCDate(20);
t.setUTCMonth(10);
The problem is a mixture of particular dates and the order of setting individual parts of the date. If you step through what's happening it becomes clearer:
New date is initialised as today: Oct 31, 2014
Setting the year to 2014: no effect
Setting the month to November: Nov 31, 2014 is interpreted as Dec 1, 2014
Setting the date to 20: Dec 20, 2014
So now, its 9:23am. I have a UTC date string that represents the current date, that looks like this "2012-07-17T09:23:27.75"
I want that in a date object, so I can display a nicely formatted date, so I:
var myDate = new Date("2012-07-17T09:23:27.75")
// Gives --> Tue Jul 17 2012 10:23:27 GMT+0100 (GMT Daylight Time)
So because of daylight saving time I'm getting an hour-out issue. I can see that myDate.getTimezoneOffset() gives me -60, what's the standard / best practice way to get my date to actually reflect the current correct time? Have I just entered javascript date hell?
Try momentjs.com. I really found it handy for such things.
var myDate = moment("2012-07-17T09:23:27.75");
Gives you a date instance in your timezone (that basically configured on your computer). Moreover momentjs has nice human friendly formattings like "a couple of seconds ago", "a month ago",...
Dates are really a hell in JS (but not only in JS). The best thing you can do is to always only transport in UTC between browser <-> server. Then on the server convert it to what time format you like, you obviously only have to be consistent. That way I managed to handle date-times properly.
Try removing the 'T'
I was debugging some date time format issue in chrome when I found out that in console
new Date('2016-04-16T15:15:00') returns Sat Apr 16 2016 16:15:00 GMT+0100 (GMT Daylight Time)
while
new Date('2016-04-16 15:15:00') returns Sat Apr 16 2016 15:15:00 GMT+0100 (GMT Daylight Time)
Could someone explain me, why I get 12 Februray when running the code below?
I saw the days are from 1 to 31, only the months starts with 0
var d = new Date(2100,1,13)
> d
Fri, 12 Feb 2100 23:00:00 GMT
EDIT:
And why this time?? 23:00:00 it should be 00:00:00
Your locale timezone is interfering. Try: new Date(Date.UTC(2100,1,13)).
You are setting 13 Feb 2100 CET and getting output in GMT.
The output is based on the GMT zone and not as per your time zone. Adjust your system time to proper time zone, you should be getting correct output. Hope that helps.