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)
Asked
Active
Viewed 84 times
-3
-
Would you like to share some code with us? – ItayB Mar 14 '15 at 15:47
-
http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript/15289883#15289883 – hpf Mar 14 '15 at 15:53
1 Answers
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
-