Traversing through all the dates in the given range of two dates

Satish
Posted by in JavaScript category on for Intermediate level | Views : 4022 red flag

Here was the challenge for me, I had two dates like FromDate and ToDate. Lets say 01/01/2007 as FromDate and 01/01/2008 as ToDate. I need to traverse through all the dates in the given period.


Initially, I was facing a lot of problem, however after many trial and error, I got it. Wish to share the code.

// Check leap year
function CheckLeap()
{
var year = document.getElementById("txtFromDate").value;
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
{
//alert("It is a Leap Year");
return true;
}
else
{
//alert("It is not a Leap Year");
return false;
}
}




// To Loop through Month
function ProcessMonths(intFDay,intTDay)
{
var strMessage;
for (i = intFDay; i <= intTDay; i++)
strMessage += i + "\n";
alert(strMessage);
}




// Main Start Function
function Process_CellClicked()
{

//debugger;
// Note for Conventions:
// v --> Var :: F --> From :: T --> To :: M --> Month :: Y --> Year
// vFM --> From Month :: vFY --> From Year
// vTM --> To Month :: vTY --> To Year

var dtFrom = new Date(document.getElementById("txtFromDate").value);

var vFD = dtFrom.getDate();
var vFM = dtFrom.getMonth()+ 1;// To Set Jan = 1
var vFY = dtFrom.getFullYear();

var dtTo = new Date(document.getElementById("txtToDate").value);

var vTD = dtTo.getDate();
var vTM = dtTo.getMonth()+ 1;// To Set Jan = 1
var vTY = dtTo.getFullYear();

var vMMax;
// Cond 1: If From Year,Month and To Year,Month are same then vMMax = vTM
// Cond 2: If From Year and To Year both are not same then vMMax = 12 and need to set a condition that vMMax = vTM for To Year

if (vFY==vTY)
vMMax=vTM;
else
vMMax=12;

for (var vYCount=vFY; vYCount<=vTY; vYCount++)
{
if(vYCount == vTY)
{
vMMax=vTM;
}

for (var vMCount=vFM; vMCount<=vMMax; vMCount++)
{
var vMaxDay;
var vMinDay;

switch(vMCount)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:

if(vMCount == vFM && vYCount == vFY)
vMinDay =vFD;
else
vMinDay = 1;

if(vMCount == vTM && vYCount == vTY)
vMaxDay = vTD;
else
vMaxDay = 31;

ProcessMonths(vMinDay,vMaxDay);
break;

case 2:

if(vMCount == vFM && vYCount == vFY)
vMinDay =vFD;
else
vMinDay = 1;

if(vMCount == vTM && vYCount == vTY)
vMaxDay = vTD;
else
{
if(CheckLeap(vYCount))
vMaxDay =29;
else
vMaxDay =28;
}


ProcessMonths(vMinDay,vMaxDay);

break;

case 4:
case 6:
case 9:
case 11:
if(vMCount == vFM && vYCount == vFY)
vMinDay =vFD;
else
vMinDay = 1;

if(vMCount == vTM && vYCount == vTY)
vMaxDay = vTD;
else
vMaxDay = 30;
ProcessMonths(vMinDay,vMaxDay);
break

}
if(vMCount==12)
{
vFM=1;
}
}
}

}
Page copy protected against web site content infringement by Copyscape

About the Author

Satish
Full Name: Satish Kumar
Member Level: Starter
Member Status: Member
Member Since: 6/29/2007 10:01:12 AM
Country:


Happy Coding...

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)