-3

what is the code for finding the difference between two date type inputs when clicking a service button in javascript ? (if admission date and retire date is given and when we click service button the service period should come as alert)

toadzky
  • 3,806
  • 1
  • 15
  • 26
kalakcika
  • 1
  • 1

1 Answers1

0

For comparing dates easily i suggest you take a look at using Moment.js

More specifically moment().diff()

From the docs here's how you would compare 2 dates and get the difference in days, i'm assuming you are using jQuery.

$('.service-button').click(function(){
    var subDate = $('.submission-date').val();
    var retireDate = $('.retirement-date').val();
    parseDateDifference(subDate, retireDate);
});

function parseDateDifference(start, end) {
    start = moment(start, 'YYYY-MM-DDD');
    end = moment(end, 'YYYY-MM-DDD');
    var difference = start.diff(end, 'days'); // this now contains you the difference in days between the dates
    alert(difference);
}
dlearious
  • 331
  • 2
  • 3
  • 9
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Himanshu Tyagi Mar 16 '15 at 06:35
  • @HimanshuTyagi i've updated as suggested thanks for the pointer. – dlearious Mar 17 '15 at 09:17