Hi all,
I need help about calculate amount of day between two dates. I want to do this in such a way:
There are two input date field from which i select date then number of days between selected date will be shown below. The code snippet look like such below:
Date1: <input type=“text” name=“fromdate” id=“fromdate” value=“” style=“width:120px;” onclick=“displayCalendar(document.add_event.fromdate,‘yyyy-mm-dd’,this)”> <font color=“#8f0000”>*</font>
<IMG onclick=“displayCalendar(document.add_event.fromdate,‘yyyy-mm-dd’,this)” style=“cursor:pointer;” src=“<?=$_path_main?>/SiteImages/cal.gif” alt=“Pick a date” width=“16” height=“16” hspace=“4” vspace=“4” border=“0” align=“absmiddle”>
<span class=“Text10BlackBold”>yyyy-mon-dd</span>
Date2: <input type=“text” name=“todate” id=“todate” value=“” style=“width:120px;” onclick=“displayCalendar(document.add_event.todate,‘yyyy-mm-dd’,this)”> <font color=“#8f0000”>*</font>
<IMG onclick=“displayCalendar(document.add_event.todate,‘yyyy-mm-dd’,this)” style=“cursor:pointer;” src=“<?=$_path_main?>/SiteImages/cal.gif” alt=“Pick a date” width=“16” height=“16” hspace=“4” vspace=“4” border=“0” align=“absmiddle”>
<span class=“Text10BlackBold”>yyyy-mon-dd</span>
Total days: (Here total days between selected days will display automatically before clicking save button finally)
Can anyone please help me about this how can i do it or is it possible?
Thanks,
Rima.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
function Days(frm,f,t,ip){
var f=frm[f].value.split(/\\D/),t=frm[t].value.split(/\\D/),days,z0;
if (f.length==3&&t.length==3&&f.join('')<t.join('')){
days=new Date(f[0],f[1],1,-1).getDate()-f[2];
t[0]-f[0]
for (z0=f[1]*1+1;z0<t[1]*1+(t[0]-f[0])*12;z0++){
days+=new Date(f[0],z0,1,-1).getDate();
}
frm[ip].value=days+t[2]*1;
}
}
/*]]>*/
</script></head>
<body>
<form>
<input name="from" value="2012-02-27" />
<input name="to" value="2012-05-02" />
<input type="button" name="" value="test" onmouseup="Days(this.form,'from','to','days');" />
<input name="days" />
</form>
</body>
</html>
Wow, that’s a highly complicated way of doing things.
Let’s simplify it to this instead:
function calculateDays() {
var form = this.form,
from = form.elements.from,
to = form.elements.to,
days = form.elements.days,
seconds = (new Date(to.value) - new Date(from.value)) / 1000;
days.value = Math.floor(seconds / 60 / 60 / 24);
}
and in a sample test page:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<form id="dateDifference">
<input name="from" value="2012-02-27">
<input name="to" value="2012-05-02">
<input type="button" id="test" value="test">
<input name="days">
</form>
<script>
function calculateDays() {
var form = this.form,
from = form.elements.from,
to = form.elements.to,
days = form.elements.days,
seconds = (new Date(to.value) - new Date(from.value)) / 1000;
days.value = Math.floor(seconds / 60 / 60 / 24);
}
var button = document.getElementById('test');
button.onclick = calculateDays;
</script>
</body>
</html>
Thanks paul_wilkins.
It is useful and i got expected result using this code.
Rima
vwphillips,
It doesn’t work properly.
Thanks,
Rima.