Subtraction of dates in JS [duplicated]

0

Request a date and return the difference in days, months and years between the current date and the date entered.

This is what I have so far to catch the current date:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();

if (dd < 10) {
    dd = '0' + dd;
}
if (mm < 10) {
    mm = '0' + mm;
}
var today = dd + '/' + mm + '/' + yyyy;
document.getElementById("DATE").value = today;

function mostrarFecha() {
    alert(today);
}
    
asked by Leo Moyano 12.04.2018 в 16:35
source

1 answer

0

You can use momentjs and specifically the moment pricise range plugin, I leave you an example of how you would do it, as a result you return an object with the values that you will need later you can format it.

var m1 = moment('2011-01-01 12:00:00','YYYY-MM-DD HH:mm:ss');
var m2 = moment('2014-02-03 15:04:05','YYYY-MM-DD HH:mm:ss');
var diff = moment.preciseDiff(m1, m2, true);
console.log(diff)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/moment.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/moment-precise-range.min.js"></script>
    
answered by 12.04.2018 в 17:50