Krishh
Krishh

Reputation: 4231

Get start and end of week with the start on Monday and end on Sunday in Javascript

    function getStartAndEndOfWeek(date) {

        var curr = date ? new Date(date) : new Date();
        var first = curr.getDate() - curr.getDay(); 

        var firstday = new Date(curr.setDate(first));
        var lastday = new Date(curr.setDate(firstday.getDate() + 6));

        return [firstday, lastday];

    }

I am unable to set the start of week as Monday. Above code gives the firstday as sunday. Also if at all we add first plus one it will give monday as the start but the issue would be where lets say (06/29/2014, sunday) is the input, it should return (06/23/2014 as start and 06/29/2014) as the end. How can we accomplish that? If I add "first + 1" it returns 06/30/2014 as start and 07/06/2014 as end date. Any help? Thanks.

Answer:

    function getStartAndEndOfWeek(date) {

        //Calculating the starting point

        var curr = date ? new Date(date) : new Date();
        var first = curr.getDate() - dayOfWeek(curr);

        var firstday, lastday;

        if (first < 1) {
            //Get prev month and year
            var k = new Date(curr.valueOf());
            k.setDate(1);
            k.setMonth(k.getMonth() - 1);
            var prevMonthDays = new Date(k.getFullYear(), (k.getMonth() + 1), 0).getDate();

            first = prevMonthDays - (dayOfWeek(curr) - curr.getDate());
            firstday = new Date(k.setDate(first));
            lastday = new Date(k.setDate(first + 6));
        } else {
            // First day is the day of the month - the day of the week
            firstday = new Date(curr.setDate(first));
            lastday = new Date(curr.setDate(first + 6));
        }

        return [firstday, lastday];            
    }

    function dayOfWeek(date, firstDay) {
        var daysOfWeek = {
            sunday: 0,
            monday: 1,
            tuesday: 2,
            wednesday: 3,
            thursday: 4,
            friday: 5,
            saturday: 6,
        };
        firstDay = firstDay || "monday";
        var day = date.getDay() - daysOfWeek[firstDay];
        return (day < 0 ? day + 7 : day);
    }

Upvotes: 7

Views: 11801

Answers (4)

much simpler

var numberdayweek = [6,0,1,2,3,4,5];
fecha = new Date();
console.log(numberdayweek[fecha.getDay()]);

Upvotes: 8

Aleksandr Skobeltcyn
Aleksandr Skobeltcyn

Reputation: 501

Found other solution:

const getDayIndexMondaySunday = (date) =>date.getDay() === 0 ? 6 : date.getDay() - 1
 // Result on Today
 console.log(getDayIndexMondaySunday(new Date()));

Upvotes: 2

Ian Clark
Ian Clark

Reputation: 9347

How about making a simple method to allow you to easily manipulate the first day of the week:

(function() {
  var original = Date.prototype.getDay;
  var daysOfWeek = {
    sunday: 0,
    monday: 1,
    tuesday: 2,
    wednesday: 3,
    thursday: 4,
    friday: 5,
    saturday: 6,
  };

  Date.prototype.getDay = function(weekBegins) {
    weekBegins = (weekBegins || "sunday").toLowerCase();
    return (original.apply(this) + 7 - daysOfWeek[weekBegins]) % 7;
  };
})();

Then you can simply use:

var first = curr.getDate() - curr.getDay("monday"),
    last  = first + 6;

Upvotes: 7

Fiddles
Fiddles

Reputation: 2915

You can set first to be the Monday by shifting the result of getDay(), then taking a modulo of the result.

var first = curr.getDate() - ((curr.getDay() + 6) % 7); 

Upvotes: 28

Related Questions