Unfortunately JavaScript doesn't have a date format function so we have to create our own:
Code:
Date.prototype.ukFormat = function(){
var days = this.getDate();
if (days<10) { days = '0'+days; }
var months = this.getMonth() + 1;//Add one since getMonth starts at 0 for January.
if (months<10) { months = '0'+months; }
var years = this.getFullYear();
return days + '/' + months + '/' + years;
}
This is a very simple one that is not flexible in the slightest, but will give you the date in the format dd/mm/yyyy.
Adding this to the script I originally posted you get:
Code:
<html>
<head>
<script type="text/javascript">
/*
return of Date().getDay()
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
*/
Date.prototype.dateFirstMonday = function(Year){
if(!Year){
var now = new Date();
Year = now.getFullYear();
}
var Firstday = new Date("January 1, "+Year+" 00:00:00");
var DayOfWeek = parseInt(Firstday.getDay());
if(DayOfWeek >= 2 && DayOfWeek <=4){
Firstday.addDays((1-DayOfWeek));
}
else if(DayOfWeek >= 5){
Firstday.addDays(7-(DayOfWeek-1));
}
else if(DayOfWeek == 0){
Firstday.addDays(1);
}
return Firstday;
}
Date.prototype.copy = function () {
return new Date(this.getTime());
};
Date.prototype.addDays = function(d) {
this.setDate( this.getDate() + d );
};
Date.prototype.addWeeks = function(w) {
this.addDays(w * 7);
};
Date.prototype.ukFormat = function(){
var days = this.getDate();
if (days<10) { days = '0'+days; }
var months = this.getMonth() + 1;//Add one since getMonth starts at 0 for January.
if (months<10) { months = '0'+months; }
var years = this.getFullYear();
return days + '/' + months + '/' + years;
}
function getWeekDates(Year, Week){
var startDate = new Date().dateFirstMonday(Year);
//To get the start date of the given week we need to add "Week-1" weeks otherwise we'd get the first day of the following week.
startDate.addWeeks(Week-1);
var weekDates = Array(7);
for (var i=0; i<=6; i++){
var thisDate = startDate.copy();
weekDates[i] = thisDate;
startDate.addDays(1);
}
return weekDates;
}
var dates = getWeekDates(2008, 32);
var msg='';
for(var i=0; i<dates.length; i++){
msg+=dates[i].ukFormat() + "\r\n";
}
alert(msg);
</script>
</head>
<body>
</body>
</html>
Hope that helps.