2

given a date object,how to get previous week's first day

John Sheehan
  • 77,456
  • 30
  • 160
  • 194

6 Answers6

6

This Datejs library looks like it can do that sort of thing relatively easily.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
5

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 what you mean by previous week's first day. I'll assume you mean previous sunday for the sake of this discussion.

To find the number of days to subtract:

  • 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 subtract is easy:

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

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

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

Note: It is valid to pass negative values to the setDate function and it will work correctly.

For the code about Monday. You have that special case because getDay() orders Sunday before Monday. So we are basically replacing getDay() in that case with a value of getDay()'s saturday value + 1 to re-order sunday to the end of the week.

We use the value of 6 for subtraction with Monday because getDay() is returning 1 higher for each day than we want.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
2

I didn't quite understand other people's posts. Here is the javascript I use to display a Sun-Sat week relative to a given day. So, for instance, to get "last week," you're checking what the Sun/Sat goalposts were relative to 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 look this up every time I need to do it. So, I hope this is helpful to others.

  • Doing it "relative to a given day" allows me to do things where the customer wants something like "a tool that will break down items completed by week," but where all I have is the completed date to work with in the data set, so the code needs to generate a set of relative week names on the fly. – undrline - Reinstate Monica May 02 '13 at 20:51
  • I have tried out your code in SailsJS and I got `TypeError: comparedate.getDay is not a function` – alexventuraio Jan 12 '16 at 01:04
  • @Lexynux, I'm not familiar with SailsJS. If you're sure that comparedate is spelled the same both times, it's possible it doesn't like subtracting 7 while creating the variable. Try something like: `var comparedate = new Date(); // today comparedate -= 7; // a week ago var dayofweek = comparedate.getDay();` – undrline - Reinstate Monica Apr 27 '16 at 15:28
2
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); 
}
Prestaul
  • 83,552
  • 10
  • 84
  • 84
1

First day of week can be either Sunday or Monday depending on what country you are in:

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 a dateobject to 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));
some
  • 48,070
  • 14
  • 77
  • 93
0

In the other examples you will have a problem when sunday falls in other 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);