Difference between two dates

Results 1 to 4 of 4

Thread: Difference between two dates

  1. #1
    Sukhwinder Singh Guest

    Default Difference between two dates

    Hello,<BR>I want to know how can I calculate the difference between two years in dates in JavaScript.<BR>I have tried this method but it doesn&#039;t claculate the right difference.<BR>date1 = new Date();<BR>date2 = new Date();<BR>diff = new Date();<BR>date1temp = new Date(objForm.DayOfMarriage.value + "/" + objForm.MonthOfMarriage.value + "/" + objForm.YearOfMarriage.value);<BR>date2temp = new Date(eval("objForm."+spouse+"_DayOfBirth.value")+"/"+eval("objForm."+spouse+"_MonthOfBirth.value" )+"/" +eval("objForm."+spouse+"_YearOfBirth.value"));<BR >date1.setTime(date1temp.getTime());<BR>date2.setT ime(date2temp.getTime());<BR>diff.setTime(Math.abs (date1.getTime() - date2.getTime()));<BR>timediff = diff.getTime();<BR>years = Math.floor(timediff / (1000 * 60 * 60 * 24 * 365));<BR><BR>Any help?

  2. #2
    Dan Evans Guest

    Default RE: Difference between two dates

    The maths is solid enough so you must be getting a dodgy value from one of your fields. Try alert()ing the 2 dates before the calculation just to make sure they are what you expect.<BR><BR>Another possible problem is that your using dates in the format MM/DD/YYYY and your system is using DD/MM/YYYY (Or vice-versa)<BR><BR>Hope this helps,<BR><BR>-Dan Evans

  3. #3
    Sukhwinder Singh Guest

    Default RE: Difference between two dates

    I am using dd/mm/yyyy and also my system date format is same. I have also checked both dates but results are not exact. Main problem is division by 365 days or 366 days.

  4. #4
    Dan Evans Guest

    Default RE: Difference between two dates

    I suppose that unless you want to work out exactly how many leap years you&#039;ve had and put that into the divide equation, you could just use 365.25 as the number of days in a year. It&#039;s just a choice between accuracy and laziness. Sadly, I go for lazy every time :o(<BR><BR>-Dan Evans

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts