1

I was using a function to return me the start and end day of a given week number from the year. Here is the function:

function getWeekRange(week){

            var d = new Date("Jan 01, " + $scope.today.getFullYear() + " 01:00:00");
            var w = d.getTime() + 604800000 * (week);
            var d1 = new Date(w);
            var d2 = new Date(w + 518400000);

            return {
                startDate: d1,
                endDate: d2,
            };

 }

I set the year dynamically, and let's see an example where the year is 2015. Considering the week number starting by 0, if I use getWeekRange(0) I will receive the following result:

{
   startDate: Thu Jan 01 2015 01:00:00 GMT-0200 (BRST), 
   endDate: Wed Jan 07 2015 01:00:00 GMT-0200 (BRST)
}

The problem is that this code does not consider the year of 2015 starting on a Thursday. The correct result for getWeekRange(0) should be:

{
   startDate: Thu Jan 01 2015 01:00:00 GMT-0200 (BRST), 
   endDate: Sat Jan 03 2015 01:00:00 GMT-0200 (BRST)
}

and the result for getWeekRange(1) should be:

{
   startDate: Sun Jan 04 2015 01:00:00 GMT-0200 (BRST), 
   endDate: Sat Jan 10 2015 01:00:00 GMT-0200 (BRST)
}

Does anyone have a clue?

-- EDIT --

My question is different from this one, because I don't have a given day of the year, I have only a week number of the year (from 0 to 51), and my case considers that the first week of the year is only a part of a full week, as mentioned by likeitlikeit.

Community
  • 1
  • 1
jgabrielfaria
  • 1,543
  • 3
  • 15
  • 19
  • 1
    This is not exactly a duplicate. However, I think your understanding of a week is wrong. A week always has 7 days, and it may span from one year to another. What you are asking for is not actually a week, but just the part of the week that falls into a given year. – likeitlikeit May 24 '15 at 08:47

2 Answers2

2

I could find a simple solution for my question, since only the first week of my year could cause me problems. Here is the code:

function getWeekRange(week){
            var d = new Date("Jan 01, " + $scope.today.getFullYear() + " 01:00:00");
            var firstWeekDays = 7 - d.getDay();
            var d1, d2;

            if(week > 0) {
                var w = d.getTime() + 604800000 * (week-1) + 24*60*60*1000 * firstWeekDays;
                d1 = new Date(w);
                d2 = new Date(w + 518400000);
            } else {
                d1 = d;
                d2 = new Date(d1.getTime() + 24*60*60*1000 * (6 - d1.getDay()));                    
            }

            return {
                startDate: d1,
                endDate: d2,
            };

    }

There are 2 main diferences from the initial code.

  • For the week of the year I simply get the initial day of the year, and then based on the day of the week it starts I can find the end day of the week

  • For the other weeks, I sum 7*week-1 days to initial day (this sum does not consider the number of days of the first week), and also add firstWeekDays which is the number of days of first week (because it is not always 7 as the other weeks).

If anyone has a better solution, I will be glad to listen.

jgabrielfaria
  • 1,543
  • 3
  • 15
  • 19
0

This answer shows how to get the first day of a week using getDate to calculate the offset from the current day.

Using this method, you can use getDate to determine which day of the week the first day of the year falls on. You can then subtract the last day of the week from this value to know how many days to add to your d2 in order to compute the date for Saturday by adjusting your date from the first day of the year + the offset in days till the end of the week for Jan 1.

Community
  • 1
  • 1
J E Carter II
  • 1,436
  • 1
  • 22
  • 39