1

I have an add date and I need to know how many days ago this record was added, so today - adddate. This is in mm/dd/yyyy format (10/16/2014) and I wanted to know if there is an easy way to get the difference without adding a new plugin. Thanks!

var today = $.datepicker.formatDate('mm/dd/yy', new Date());
var adddate = $('#adddate').val();

alert(today - adddate);
3
  • 1
    Sounds similar to my question: stackoverflow.com/questions/12661293/… Meaning that you have to parseDate before being able to subtract it.
    – XCS
    Oct 16, 2014 at 13:49
  • Just convert both dates to milliseconds and do the subtraction manually. Convert back to number of days after subtraction. Oct 16, 2014 at 13:50
  • How do I convert to milliseconds and convert back? Oct 16, 2014 at 14:01

1 Answer 1

1

Assuming both values are inside inputs, this works.

var d1 = $('#adddate').datepicker('getDate');
var d2 = $('#today').datepicker('getDate');
var diff = 0;

    if (d1 && d2) {
        diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day
    }

    alert(diff)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.