
February 8th, 2008, 07:04 PM
|
 |
Contributing User
|
|
Join Date: Nov 2003
Location: UK
|
|
|
JavaScript - Javascript function to get each monday in month
here is one way to get each monday in the current month.
please note this is for UK hense the UTC dates.
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get each monday in the current month</title>
<script language="javascript">
<!--//
// currentDateStart is the 1st of the current month
var currentDateStart = new Date;
currentDateStart.setDate(1);
currentDateStart.getUTCMonth();
var m = currentDateStart.getUTCMonth();
currentDateStart.getUTCFullYear();
var y = currentDateStart.getUTCFullYear();
// currentDateEnd is the 1st of the next month
var currentDateEnd = new Date;
currentDateEnd.setDate(1);
var endMonth = currentDateStart.getUTCMonth();
var endYear = currentDateStart.getUTCFullYear();
if (endMonth < 10) {
endMonth += 1;
}
else {
endMonth = 1;
endYear += 1;
}
currentDateEnd.setMonth(endMonth);
currentDateEnd.setFullYear(endYear);
// once you have the 1st of next month you need to subtract one day
// to get the date of the last day of the current month you need to
// subtract 86400000 milliseconds which is the value for one day
// this will save time leap years ect.
var endDate = new Date( currentDateEnd.getTime() - 86400000 );
var endDateDay = endDate.getDate();
// this function loops through each day and finds the mondays
// and places them in to an array
var mondayArray = new Array();
var c = 0;
function getMondayArray() {
var loopDate = new Date;
var dayName;
var lDay;
var lMonth;
for (var d=1;d<=endDateDay;d++) {
loopDate.setDate(d);
loopDate.setMonth(m);
loopDate.setFullYear(y);
dayName = loopDate.getDay();
// the getDay value for monday is 1
if (dayName == 1) {
c += 1;
lDay = loopDate.getDate();
if (lDay < 10) lDay = "0" + lDay;
lMonth = (loopDate.getMonth() + 1)
if (lMonth < 10) lMonth = "0" + lMonth;
mondayArray[c] = (lDay + "-" + lMonth + "-" + loopDate.getFullYear());
}
}
}
// this function collects the data and displays it on the page
function displayResults() {
var headerSpan = "";
var resultsSpan = "";
var sDay = currentDateStart.getDate();
if (sDay < 10) sDay = "0" + sDay;
var sMonth = (currentDateStart.getMonth() + 1);
if (sMonth < 10) sMonth = "0" + sMonth;
headerSpan += ("<h2>Date Range: 01" + "-" + sMonth + "-" + currentDateStart.getFullYear() + " to ");
var eMonth = (endDate.getMonth() + 1);
if (eMonth < 10) eMonth = "0" + eMonth;
headerSpan += (endDate.getDate() + "-" + eMonth + "-" + endDate.getFullYear() + "</h2>");
resultsSpan += ("<b>RESULTS: " + c + "</b>");
for (var r=1;r<=c;r++) {
resultsSpan += ("<br />" + mondayArray[r]);
}
document.getElementById("headerSpan").innerHTML = headerSpan;
document.getElementById("resultsSpan").innerHTML = resultsSpan;
}
function show_dates() {
getMondayArray();
displayResults();
}
//-->
</script>
</head>
<body>
<a href="javascript:show_dates();">show date results</a>
<p> </p>
<span id="headerSpan"></span>
<span id="resultsSpan"></span>
</body>
</html>
__________________
Hope this advise helps.
If so please show your appreciation by adding reputation points (clcik gauge image on top right of this post).
|