How to get the first day of the previous week from a date object in JavaScript?

set date object how to get the previous week on the first day

+2
source share
6 answers

This Datejs library looks like it can do this relatively easily.

+6
source

The code:

function getPreviousSunday() { var today=new Date(); return new Date().setDate(today.getDate()-today.getDay()-7); } function getPreviousMonday() { var today=new Date(); if(today.getDay() != 0) return new Date().setDate(today.getDate()-7-6); else return new Date().setDate(today.getDate()-today.getDay()-6); } 

Reasoning:

Depends on what you mean the previous day on the first day. I assume that you mean the previous Sunday for this discussion.

To find the number of days to deduct:

  • Get the current day of the week.
  • If the current day of the week is Sunday, you subtract 7 days
  • If the current day is Monday, you subtract 8 days

...

  • If the current day is Saturday 13 days

The actual code, once you determine the number of days to deduct, is easy:

 var previous_first_day_of_week=new Date().setDate(today.getDate()-X); 

Where X is the above value. This value is today.getDay () + 7

If you meant something else on the first day of the week, you should be able to deduce the answer from the steps above.

Note. Indeed, to pass negative values ​​to the setDate function, it will work correctly.

Code on Monday. You have this special case, because getDay () orders are Sunday through Monday. Thus, we basically replace getDay () in this case with the value of getDay () equal to half a day + 1, for re-ordering Sunday until the end of the week.

We use a value of 6 to subtract from Monday, because getDay () returns 1 more for each day than we want.

+5
source
 function previousWeekSunday(d) { return new Date(d.getFullYear(), d.getMonth(), d.getDate() - d.getDay() - 7); } function previousWeekMonday(d) { if(!d.getDay()) return new Date(d.getFullYear(), d.getMonth(), d.getDate() - 13); return new Date(d.getFullYear(), d.getMonth(), d.getDate() - d.getDay() - 6); } 
+2
source

I did not quite understand the messages of other people. Here is the javascript that I use to display the Sun-Sat week relative to that day. So, for example, to get β€œlast week,” you check which Sun / Sat goalposts were relative seven days ago: new Date()-7

  // variables var comparedate = new Date()-7; // a week ago var dayofweek = comparedate.getDay(); // just for declaration var lastdate; var firstadate; // functions function formatDate (dateinput) // makes date "mm/dd/yyyy" string { var month = dateinput.getMonth()+1; if( month < 10 ) { month = '0' + month } var date = dateinput.getDate(); if( date < 10 ) { var date = '0' + date } var dateoutput = month + '/' + date + '/' + dateinput.getFullYear(); return dateoutput; } // Sunday to Saturday ... Sunday is the firstdate, Saturday is the lastdate // (modify this block if you want something different eg: Monday to Sunday) if ( dayofweek == 6 ) { lastdate = comparedate; firstdate = comparedate-6; } // Saturday else if ( dayofweek == 0 ) { lastdate = comparedate+6; firstdate = comparedate; } // Sunday else if ( dayofweek == 1 ) { lastdate = comparedate+5; firstdate = comparedate-1; } // Monday else if ( dayofweek == 2 ) { lastdate = comparedate+4; firstdate = comparedate-2; } // Tuesday else if ( dayofweek == 3 ) { lastdate = comparedate+3; firstdate = comparedate-3; } // Wednesday else if ( dayofweek == 4 ) { lastdate = comparedate+2; firstdate = comparedate-4; } // Thursday else if ( dayofweek == 5 ) { lastdate = comparedate+1; firstdate = comparedate-5; } // Friday // Finish var outputtowebpage = formatDate(firstdate) + ' - ' + formatDate(lastdate); document.write(outputtowebpage); 

I have to watch it every time I need it. Therefore, I hope that it will be useful for others.

+2
source

The first day of the week may be Sunday or Monday, depending on the country in which you are:

 function getPrevSunday(a) { return new Date(a.getTime() - ( (7+a.getDay())*24*60*60*1000 )); }; function getPrevMonday(a) { return new Date(a.getTime() - ( (6+(a.getDay()||7))*24*60*60*1000 )); }; 

If you want to set the date object on the previous Sunday, you can use:

 a.setDate(a.getDate()-7-a.getDay()); 

and for the previous monday:

 a.setDate(a.getDate()-6-(a.getDay()||7)); 
+1
source

In other examples, you will have a problem when Sunday passes in another month. This should solve the problem:

 var today, todayNumber, previousWeek, week, mondayNumber, monday; today = new Date(); todayNumber = today.getDay(); previousWeek = -1; //For every week you want to go back the past fill in a lower number. week = previousWeek * 7; mondayNumber = 1 - todayNumber + week; monday = new Date(today.getFullYear(), today.getMonth(), today.getDate()+mondayNumber); 
0
source

All Articles