Helban,
Try this :-
function getWeekNo(startDay, baseDate){
var now = new Date();
startDay = (!startDay) ? 0 : startDay;//Default to Sunday
baseDate = (!baseDate) ? new Date(now.getYear(), 0, 1) : baseDate;//Default to 1st Jan of current year.
if(startDay !== -1){
while(baseDate.getDay() !== startDay){ baseDate.setTime(baseDate.getTime() - (24 * 3600 * 1000)); }
}
return Math.ceil((now.getTime() - baseDate.getTime())/(7 * 24 * 3600 * 1000));
} As it depends on exactly what you mean by "week number", I have made the code flexible. The function accepts two parameters, as follows:startDay : The day which should be used as the first day of each week. 0=Sunday, 1=Monday etc. Pass -1 to use the raw baseDate as week start, regardless of what day it is. Default = 0;
baseDate : The date to be used as the first day of the year. Default = 1st January of the current year.
Sample calls:
var weekNo = getWeekNo();//Conventional week numbering based on calender year, each week starting on Sunaday.
var weekNo = getWeekNo(1);//Conventional week numbering based on calender year, each week starting on Monday.
showWeekNo( 6, new Date(new Date().getYear(), 3, 1) );//Week number of UK corporate fiscal year (1 April to 31 March), starting on Saturday (as used by some companies). I've done some rudimtary testing and for some reason it waits untill 1:00 am on the first day of each week to throw a new week number, rather than the 00:00 (midnight) as you would expect. I can't immediately see why this should be. The rest of the time it seems to behave properly.
Please don't use my function in anything safety/financially/etc. critical.
For text, just set up an array of strings :-
var txtArray = [];
txtArray[1] = 'On no, back to work, I\'m still a bit hung over';
txtArray[2] = 'These dark nights are getting to me';
txtArray[3] = 'Brrrr, had to scrape ice off my bicycle saddle this morning';
txtArray[n] = 'xxxxxxx';
txtArray[52] = 'Oh no, it\'s the Midwinter Retail Festival!';
txtArray[53] = 'New year is upon us again'; Then index into this array with the week number returned by getWeekNo() . eg.
weekMessage = txtArray[getWeekNo()];
Airshow