Click to See Complete Forum and Search --> : JavaScript -> Week of the Month?


thejoker101
01-13-2003, 09:54 AM
I need a script that will do this, but I haven't had much luck writing it or finding one online, any help?

gil davis
01-13-2003, 10:15 AM
Here's one that will tell the week of the year, and the week of the month:

<script>
var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
today = new Date();
// How many milliseconds since Jan-01-1970?
thisDay = Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate());
// How many days since Jan-01-1970?
thisDay = thisDay / (24 * 60 * 60 * 1000);
// What day of year is today?
thisDay = thisDay - Math.round((today.getUTCFullYear() - 1970) * 365.25);
// What day of week was Jan-01?
offset = new Date(today.getUTCFullYear(), 0, 1);
offset = offset.getDay();
thisDay = thisDay + offset;
// Which week of the year are we in?
thisWeek = Math.round(thisDay / 7) + 1;
// What is the correct trailer (st, nd, th) for the number?
twAdj = (thisWeek % 10 == 1) ? "st" : (thisWeek % 10 == 2) ? "nd" : (thisWeek % 10 == 3) ? "rd" : "th";
alert("This is the " + thisWeek + twAdj + " week of " + today.getUTCFullYear());
// What day of week was the first of the month?
offset = new Date(today.getUTCFullYear(), today.getMonth(), 1);
// Which week of the month are we in?
thisWeek = Math.round(thisDay / 7) + 1;
// What is the correct trailer (st, nd, th) for the number?
twAdj = (thisWeek % 10 == 1) ? "st" : (thisWeek % 10 == 2) ? "nd" : (thisWeek % 10 == 3) ? "rd" : "th";
alert("This is the " + thisWeek + twAdj + " week of " + months[today.getMonth()]);
</script>

thejoker101
01-13-2003, 10:19 AM
thanks, i'll have a look at it...