I want to get week Start Date of a week. I am able to get the date, its just that the date returned has time wrt to the current system time. For example, if its 19:20 hours right now, I am getting the week start date as Date {Mon Mar 26 2012 19:20:16 GMT+0530 (IST)}
For accurate calculations, I need the time to be 00:00:00 GMT.
How can I achieve that?
My current code looks like this:
var tDate = new Date();
var wDate = new Date();
this.chosenDayIndex = this.currentDayIndex = tDate.getDay();
if (this.currentDate == '') {
this.currentDate = new Date();
var today = formatDate(tDate);
var diff = tDate.getDate() - this.currentDayIndex + (this.currentDayIndex == 0 ? -6:1);
this.weekStartDate = this.weekMonDate = new Date(wDate.setDate(diff));
this.weekEndDate = new Date(this.weekStartDate.getFullYear(),this.weekStartDate.getMonth(), this.weekStartDate.getDate()+6);
this.weekMonday = formatDate(this.weekMonDate);
}
this.weekStartDate is currently handing out the false start date with the current timestamp.
I want to get week Start Date of a week. I am able to get the date, its just that the date returned has time wrt to the current system time. For example, if its 19:20 hours right now, I am getting the week start date as Date {Mon Mar 26 2012 19:20:16 GMT+0530 (IST)}
For accurate calculations, I need the time to be 00:00:00 GMT.
How can I achieve that?
My current code looks like this:
var tDate = new Date();
var wDate = new Date();
this.chosenDayIndex = this.currentDayIndex = tDate.getDay();
if (this.currentDate == '') {
this.currentDate = new Date();
var today = formatDate(tDate);
var diff = tDate.getDate() - this.currentDayIndex + (this.currentDayIndex == 0 ? -6:1);
this.weekStartDate = this.weekMonDate = new Date(wDate.setDate(diff));
this.weekEndDate = new Date(this.weekStartDate.getFullYear(),this.weekStartDate.getMonth(), this.weekStartDate.getDate()+6);
this.weekMonday = formatDate(this.weekMonDate);
}
this.weekStartDate is currently handing out the false start date with the current timestamp.
Share Improve this question edited Sep 29, 2023 at 20:17 Mark Schultheiss 34.2k12 gold badges72 silver badges111 bronze badges asked Mar 30, 2012 at 13:55 amitamit 10.3k23 gold badges76 silver badges125 bronze badges 2 |4 Answers
Reset to default 12You can set the date to the start of the week with this:
var d = new Date();
d.setDate(d.getDate() - d.getDay());
Then use .setHours(0)
, .setMinutes(0)
, etc. to clear out the time.
if you start your week on Monday,
d.setDate(d.getDate()-(d.getDay() ? d.getDay()-1 : 6));
d.setHours(0, 0, 0, 0);
You'll want to work with the UTC versions of the date getter functions.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getUTCDate
That will get you the day based on utc, rather than local time.
You can return the local time string or the GMT time string for any Date object by adjusting the local time accordingly:
// 1. Monday(00:00:00 Local time)
var mon= new Date();
mon.setHours(0, 0, 0, 0);
var d= mon.getDate();
while(mon.getDay()!= 1) mon.setDate(--d);
alert(mon.toLocaleString());
/* returned value: (String)
Monday, March 26, 2012 12:00:00 AM
*/
// 2. Monday(00:00:00 GMT)
var umon= new Date();
umon.setUTCHours(0, 0, 0, 0);
var d= umon.getUTCDate();
while(umon.getUTCDay()!= 1) umon.setUTCDate(--d);
alert(umon.toUTCString())
/* returned value: (String)
Mon, 26 Mar 2012 00:00:00 GMT
*/
In this example the two dates with the same basic string are 4 hours apart.
setHours()
,setMinutes()
,setSeconds()
, etc. – Pointy Commented Mar 30, 2012 at 13:57