Da Sie nicht wollen, zusätzliche Plug-in hinzufügen Sie Modulus-Operator verwenden können %
Monate zu konvertieren Wert zu korrigieren.
Hier ist ein funktionierendes Beispiel:
var todaysDate = moment();
var toDate = moment().add(14, 'months');
var years = toDate.diff(todaysDate, 'years');
var months = toDate.diff(todaysDate, 'months');
console.log(years + ' years, ' + months % 12 + ' months');
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
Eine andere Lösung moment-duration-format verwendet, ist die folgende:
var todaysDate = moment();
var toDate = moment().add(14, 'months');
var months = toDate.diff(todaysDate, 'months');
var duration = moment.duration(months, 'months');
console.log(duration.format("y [years] M [months]"));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
Auf diese Weise erstellen Sie ein Dauerobjekt aus dem Wert monates und formatieren es anschließend mit dem Plug-In nach Ihren Bedürfnissen.
Die Dokumentation scheint die Verwendung von ['from()'] anzuzeigen (http://momentjs.com/docs/#/displaying/from/) ... –
Mögliche Duplikate von [Wie man den Unterschied zwischen 2 Termine in Jahren, Monaten und Tagen mit moment.js] (http://stackoverflow.com/questions/26063882/how-to-get-difference-between-2-dates-in-years-months-and-days-using -moment-js) –
@MikeMcCaughan die angenommene Antwort für diese Frage erfordert ein zusätzliches Plugin. Was ich nicht gebrauchen kann. – Daft