0

I'm looking for some help with my coursework as I've spent the last 8 hours trying to find a way to do what i'm trying to accomplish.

Now I have to use JavaScript, cannot use plugins but i can use the jQuery Library.

I'm basically trying to assign weeks to the semesters of the university year, so we say 29th September 2014 (Monday) to 26th January which is around 15 weeks.

Now I'm using something like this:

  Date.prototype.getWeek = function() {
    var firstJan = new Date(2014,0,1);
    var today = new Date(2014, 9, 14);
    var firstMonSem1 = new Date(2014, 8, 29);
    var firstMonSem2 = new Date(this.getFullYear(), 1, 1);
    var dayOfYear = ((today - firstJan + 86400000)/86400000);
    var startWeekSem1 = ((firstMonSem1 - firstJan + 86400000)/86400000);
    var startWeekSem2 = ((firstMonSem2 - firstJan + 86400000)/86400000);
    var currentWeekNumber = Math.ceil(dayOfYear/7);
    var startWeekNumberSem1 = Math.ceil(startWeekSem1/7);
    var startWeekNumberSem2 = Math.ceil(startWeekSem2/7);

    var output = {current: currentWeekNumber, sem1: startWeekNumberSem1, sem2: startWeekNumberSem2};

    return output;

  };

So i'm returning the current week of the year, semester start date for semester 1 and 2.

But for this example let's only focus on the first is 29th September (Monday) until 26th January which is around 15 weeks.

Now i've worked out based on the values i get when the semester starts and finishes including a 4 week holiday over the christmas period. And then I have an array for the index of which week of the semester we are in, in comparison to the week of the year (this way it could be week 41 in the year and based on the index array I would know that this is week 2).

Here's the code (note: it works!)

var today = new Date();
var info = today.getWeek();

if (info['current'] >= info['sem1'] && info['current'] <= (info['sem1'] + 18)) {
  // Semester 1
  var index = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
  var yearWeeks = [];

  var count = 1;
  for (var i = info['sem1']; i < (info['sem1'] + 18); i++) {
    if (count !== 12 && count !== 13 && count !== 14) {
      yearWeeks.push(i);
    }
    count++;
  }

  var rightIndex = $.inArray(info['current'], yearWeeks);

  var x = 1;
  while (x <= 15) {
    if (x >= rightIndex) {
      var weekOption = $("<option/>", {
         "value" : x,
         html : "Week "+x
        }).appendTo(week);
    }
    x++;
  }
}

So what I do here is append to a select all the weeks that are greater than the current week, so if we are in week 5 then there won't be a week 1,2,3,4 for the sake of this example (it's because in the project we are selecting a week to make a booking on but that's irrelevant).

Now, the problem.

This all works fantastically but there is a major catch, the week changes on Wednesday for example:

Week 1 is commencing 29th of September (Monday) Week 2 starts 8th October (Wednesday) Week 3 starts 15th October (Wednesday)

So every time the week changes on the Wednesday, now as the idea is that this system is being implemented so you can book room for that given week aka Monday - Friday, I need the weeks to update every Monday rather than Wednesday.

I really need help with this, and I know it might be a lot to grasp. However, I appreciate all the help I can get.

Thank you.

isherwood
  • 58,414
  • 16
  • 114
  • 157
mdixon18
  • 1,199
  • 4
  • 14
  • 35
  • Is the time on your computer correct? – Daniel Feb 13 '15 at 06:35
  • Yes it is, but I would like the weeks to update on the Sunday rather than Wednesday :( – mdixon18 Feb 13 '15 at 06:38
  • You state `currentWeekNumber = Math.ceil(dayOfYear/7);` .. That is a wild assumption to say your first week starts at day 1. I feel there is some math problem there. The first of the year just happens to be on Thursday. Is that giving you ideas? (i'm off, just hope to help you along a bit) – Daniel Feb 13 '15 at 06:43
  • okay, don't know how to fix it (mathmatically) – mdixon18 Feb 13 '15 at 06:45
  • Maybe teh info here will help: http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php – Daniel Feb 13 '15 at 06:47

2 Answers2

0

I think you need to replace this line:

var currentWeekNumber = Math.ceil(dayOfYear/7);

To be:

var currentWeekNumber = Math.ceil((dayOfYear + 5)/7);

To make a week start from Wednesday. In such case, the week of Day 2 (Tuesday) is Math.ceil((2+5)/7)=1, while Day 3 (Wednesday) is Math.ceil((3+5)/7)=2.

Joy
  • 9,430
  • 11
  • 44
  • 95
0

I worked it out some and hope it helps. This is not just for 2014 that you use, but for the current year. With some adaptation you can adjust. You get the first of the year and get the offset of the day-number. Then you calculate the current day and subtract. The result will help you get the weeknr. The week starts on Monday (where in other regions it may be on Sunday).

I created a fiddle, so you can play with it: http://jsfiddle.net/djwave28/r0m4sokv/

This is an abstract from that fiddle:

// set an offset

var offset = 0;
var now = new Date();
var testday = new Date(now.getTime() + offset * 24 * 60 * 60 * 1000);
var firstofyear = new Date(testday.getFullYear(), 0, 1);
var firstdayoffset = firstofyear.getDay();
var daynr = Math.ceil((testday - firstofyear) / 86400000);
var weeknr = (daynr - firstdayoffset) / 7;
Daniel
  • 4,816
  • 3
  • 27
  • 31