3

I need to get the first and last day of a specific calendar week in JavaScript. I want to pass week and year to a function. I am thinking about a function who looks like this one.

    getBeginEndOfCalweek: function(ww, yyyy) { 

?
    return beginEnd [ yyyy-mm-dd , yyyy-mm-dd] 
    }

I want to call that function with a specific week and year. Like this.

var a[] = getBeginEndOfCalweek(48,2017);

Every help is very much appreciated. Best Peter

Peter S.
  • 31
  • 1
  • SO isn't a free code writing service. What research have you done? What have you tried so far? This question will soon get closed as off-topic, so better add some code you tried. –  Nov 30 '17 at 10:30
  • Take help for research from here : https://stackoverflow.com/questions/16590500/javascript-calculate-date-from-week-number – D. Pareek Nov 30 '17 at 10:31
  • I wanted to use the following function. But this works only for the actual day. getWeekDates: function() { var day_milliseconds = 24 * 60 * 60 * 1000; var dates = []; var current_date = new Date(); var monday = new Date(current_date.getTime() - (current_date.getDay() - 1) * day_milliseconds); var sunday = new Date(monday.getTime() + 6 * day_milliseconds); dates.push(monday); for (var i = 1; i < 6; i++) { dates.push(new Date(monday.getTime() + i * day_milliseconds)); } dates.push(sunday); return dates; }, – Peter S. Nov 30 '17 at 10:37
  • Instead of week number, do you have date for that you want to find first and last day of that date's week? I need specific date. – Chandrakant Thakkar Nov 30 '17 at 10:37
  • Thats exactly the problem I have. But I only get calweek. – Peter S. Nov 30 '17 at 10:38

1 Answers1

1

I use the function of an other Peter in this Answer

// Function from https://stackoverflow.com/users/2881466/peter
// https://stackoverflow.com/a/19375264/5413817
function firstDayOfWeek (week, year) {

    // Jan 1 of 'year'
    var d = new Date(year, 0, 1),
        offset = d.getTimezoneOffset();

    // ISO: week 1 is the one with the year's first Thursday 
    // so nearest Thursday: current date + 4 - current day number
    // Sunday is converted from 0 to 7
    d.setDate(d.getDate() + 4 - (d.getDay() || 7));

    // 7 days * (week - overlapping first week)
    d.setTime(d.getTime() + 7 * 24 * 60 * 60 * 1000 
        * (week + (year == d.getFullYear() ? -1 : 0 )));

    // daylight savings fix
    d.setTime(d.getTime() 
        + (d.getTimezoneOffset() - offset) * 60 * 1000);

    // back to Monday (from Thursday)
    d.setDate(d.getDate() - 3);

    return d;
}

function getBeginEndOfCalweek(week, year) { 
  var result = {
        'start': new Date(),
        'end': new Date(),

    'toString': function(){
        return 'start: '+this.start+'<br>end: '+this.end;
    }
  };
  result.start = firstDayOfWeek(week, year);
  result.end.setDate(result.start.getDate() + 6)

  return result; 
}

console.log(getBeginEndOfCalweek(48,2017).toString());

This will output (in Germany):

start: Mon Nov 27 2017 00:00:00 GMT+0100 (Mitteleuropäische Zeit)
end: Sun Dec 03 2017 12:13:22 GMT+0100 (Mitteleuropäische Zeit)
Marcus
  • 1,910
  • 2
  • 16
  • 27
  • Hi Marcus, many thanks for your help. The function firstDayOfWeek (week, year) works perfectly. – Peter S. Dec 05 '17 at 07:35
  • This was not my function. It is from this [Answer](https://stackoverflow.com/a/19375264/5413817) by [Peter](https://stackoverflow.com/users/2881466/peter) – Marcus Dec 05 '17 at 08:14