I have an angular.js application, and I want to get the dates of Monday and Sunday of the current week, which are then set to calendar dates by default. I was looking at an error due to the produced dates not falling within 7 days time span. I fixed the issue.
But as I was playing around with the code I found out that, similar date creation code was producing different values for hour. Here is a snippet of my code:
var current = new Date(); // get current date
var weekstart = current.getDate() - current.getDay() + 1;
var weekend = weekstart + 6; // end day is the first day + 6
var firstday = new Date(current.setDate(weekstart));
firstday = new Date(firstday.getFullYear() + '-' + (firstday.getMonth() + 1)
+ '-' + firstday.getDate());
console.log("Monday: " + firstday);
//firstday.setHours(0, 0, 0, 0);
var last = new Date(firstday);
last.setDate(last.getDate() + 6);
console.log("Sunday 1: " + last);
var lastday = new Date(last.getFullYear() + '-' + (last.getMonth() + 1)
+ '-' + last.getDate());
console.log("Sunday 2: " + lastday);
Here are the values generated in my console:
Monday: Mon Oct 09 2017 00:00:00 GMT+1100 (AUS Eastern Daylight Time)
Sunday 1: Sun Oct 15 2017 00:00:00 GMT+1100 (AUS Eastern Daylight Time)
Sunday 2: Sun Oct 15 2017 11:00:00 GMT+1100 (AUS Eastern Daylight Time)
Why is the Hour field in Sunday 2 getting generated as 11, whereas similar new Date call for Monday resulted in 00?