user2239933
user2239933

Reputation: 99

Scripts that get weeknumber, but changes to the next week every friday

I have a screen running and showing the jobs of the current week. I use this script to calculate current week:

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(),0,1);
    return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
} 

But I would like it to show next week every friday. Is there an easy way to change the current script so it changes on friday instead? Maybe a way to get it to change at 10 am (my timezone - CEST)

All hints and pointers are greatly appreciated :)

Upvotes: 3

Views: 61

Answers (1)

Alexandr Lazarev
Alexandr Lazarev

Reputation: 12872

If I understood you correctly you can simply increment reutrned value if today is friday.

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(),0,1);        
    var weekNr = Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
    return this.getDay() == 5 ? weekNr + 1 : weekNr;
}

Keep in mind that getDay() returns the day of the week (from 0 to 6) for the specified date. So Sunday 0, Monday is 1 and Friday is 5. You can get more details here.

Upvotes: 1

Related Questions