PDA

View Full Version : need help to count days


6arredja
12-12-2005, 02:56 PM
:o :eek: :( :mad: i need help to count how many days there are between two dates such as 03/12/05 and 07/23/05 is there a javascript function that will give me the number of days in each month or am i going to have to write some code?:)

vwphillips
12-12-2005, 03:15 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>
03/12/05 and 07/23/05
<script language="JavaScript" type="text/javascript">
<!--
var Date1=new Date(2005,03,12);
var Date2=new Date(2005,07,23);
var days=(Date2-Date1)/1000/60/60/24
alert(days)
//-->
</script>
<body>

</body>

</html>

Javascript aslready knows how many days there are in a month.

vwphillips
12-12-2005, 03:31 PM
to demonstrate

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>
<input id="Days" ><br>

<input id="Month" >

<input type="button" id="DaysinMonth" value="Enter a Month and Press Me" onclick="DaysInMonth()" ><br>



<script language="JavaScript" type="text/javascript">
<!--
var Date1=new Date(2005,03,12);
var Date2=new Date(2005,07,23);
document.getElementById('Days').value=(Date2-Date1)/1000/60/60/24+' Days';

function DaysInMonth(){
obj=document.getElementById('Month');
m=obj.value;
if (m==''||isNaN(m)){alert('Only Digits'); return; }
if (m<1||m>12){ alert('Invalid Month'); return; }
// JS Months 0=Jan, 11=Dec
// the new date below is the first day of the next month -1 hour which is the last day specified month
// .getDate returns the day number
obj.value=new Date(2005,m,1,-1).getDate();
}

//-->
</script>
<body>

</body>

</html>