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

Posted by in JavaScript category on for Intermediate level | Views : 5234

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(&amp;quot;txtFromDate&amp;quot;).value;
if ((year % 4 == 0) &amp;amp;&amp;amp; (year % 100 != 0) || (year % 400 == 0))
{
return true;
}
else
{
//alert(&amp;quot;It is not a Leap Year&amp;quot;);
return false;
}
}

// To Loop through Month
function ProcessMonths(intFDay,intTDay)
{
var strMessage;
for (i = intFDay; i &amp;lt;= intTDay; i++)
strMessage += i + &amp;quot;\n&amp;quot;;
}

// Main Start Function
function Process_CellClicked()
{

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

var dtFrom = new Date(document.getElementById(&amp;quot;txtFromDate&amp;quot;).value);

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

var dtTo = new Date(document.getElementById(&amp;quot;txtToDate&amp;quot;).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&amp;lt;=vTY; vYCount++)
{
if(vYCount == vTY)
{
vMMax=vTM;
}

for (var vMCount=vFM; vMCount&amp;lt;=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 &amp;amp;&amp;amp; vYCount == vFY)
vMinDay =vFD;
else
vMinDay = 1;

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

ProcessMonths(vMinDay,vMaxDay);
break;

case 2:

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

if(vMCount == vTM &amp;amp;&amp;amp; 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 &amp;amp;&amp;amp; vYCount == vFY)
vMinDay =vFD;
else
vMinDay = 1;

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

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

}```

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.