Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
Hi Dear Friend.
 
I have Use this Code.
function ddiff() {
 
           var txtfromdate = document.getElementById('<%=txtFromDate.ClientID %>').value;
           var txttodate = document.getElementById('<%=txtToDate.ClientID %>').value;
 
           var oneDay = 1000 * 60 * 60 * 24; //Convert into millisec
           var firstDate = new Date(a);
           var secondDate = new Date(b);
 
           var noOfDays = Math.ceil((secondDate.getTime() - firstDate.getTime()) / oneDay);
           document.getElementById('<%=txtLeaveRequired.ClientID %>').value = noOfDays;
 

       }

Find date Diff. in text box but not proper Anwser.
 
For Eg.1:-txtFromDate :- 25/3/2013
2:-txtToDate:- 26/3/2013.
And Get 31 Day..but true Ans 1 Day..
 
So fast help me..
Posted 28-Mar-13 0:35am
Achal Oza1.4K
Comments
onurag19 at 28-Mar-13 6:03am
   
In which format do you need to find the date? I mean days, years etc.
onurag19 at 28-Mar-13 6:05am
   
http://ajitmahajani.wordpress.com/2012/08/05/my-experiments-with-date-diff-in-javascript/
Please check this.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello Achal,
 
The new Date(dateString) constructor accepts string representing an RFC2822 or ISO 8601 date. So make sure that values a & b are in a valid format. In your case the format seems to be dd/mm/yyyy, where as according to the rules it should be mm/dd/yyyy and hence you are getting the difference as 31 days. The date values you have given will get converted as Jan 3, 2015 & Feb 3, 2015 respectively (dd/mm/yyyy format).
 
Here is a sample
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function dateDiff() {
   var dtFrom = document.getElementById('txtFromDate').value;
   var dtTo = document.getElementById('txtToDate').value;
 
   var dt1 = new Date(dtFrom);
   var dt2 = new Date(dtTo);
   var diff = dt2.getTime() - dt1.getTime();
   var days = diff/(1000 * 60 * 60 * 24);
   alert(dt1 + ", " + dt2);
 
   alert(days);
   return false;
}
function isNumeric(val) {
   var ret = parseInt(val);
}
</script>
</head>
<body>
   <form id="frmMain" name="frmMain" method="POST">
       <label for="txtFromDate">From Date : </label>
       <input type="text" id="txtFromDate" name="txtFromDate" size="10" maxlength="10" value="03/25/2013"/><br/>
       <label for="txtToDate">To Date : </label>
       <input type="text" id="txtToDate" name="txtDate" size="10" maxlength="10" value="03/26/2013"/><br/>
       <button id="btnCheck" name="btnCheck" onClick="dateDiff();" type="button">Difference</button>
   </form>
</body>
</html>
Regards,
  Permalink  
v2
Comments
Achal Oza at 28-Mar-13 6:08am
   
Ya..but how to change Date Format..?
If i m change then display some error.
Prasad Khandekar at 28-Mar-13 6:16am
   
If you read my response or ECMA Spec. you will know that for Date Constructor the date string has to be in specified format. Your own format can be anything.
Achal Oza at 28-Mar-13 6:29am
   
k..i have Sollow.
 
function ddiff() {
 
var txtfromdate = document.getElementById('<%=txtFromDate.ClientID %>').value;
var txttodate = document.getElementById('<%=txtToDate.ClientID %>').value;
var txtfromdate_Change = txtfromdate.split("/");
var txttodate_Change = txttodate.split("/");

txtfromdate = txtfromdate_Change[1] + "/" + txtfromdate_Change[0] + "/" + txtfromdate_Change[2];
txttodate = txttodate_Change[1] + "/" + txttodate_Change[0] + "/" + txttodate_Change[2];

var oneDay = 1000 * 60 * 60 * 24; //Convert into millisec
var firstDate = new Date(txtfromdate);
var secondDate = new Date(txttodate);
 
var noOfDays = Math.ceil((secondDate.getTime() - firstDate.getTime()) / oneDay);
document.getElementById('<%=txtLeaveRequired.ClientID %>').value = noOfDays;
 

}
Prasad Khandekar at 28-Mar-13 6:37am
   
Yes it should work.
Achal Oza at 28-Mar-13 8:01am
   
Thankx.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 245
1 Kamal Rocks 184
2 Sergey Alexandrovich Kryukov 175
3 BillWoodruff 173
4 PIEBALDconsult 160
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100