2017-06-29 1 views
0

Probleme zu haben Tagen Moment js Objekte hinzufügen:hinzufügen Tagen mit Moment JS

Ich verwende diesen Code:

var contractMoment = this.moment(contract,'DD/MM/YYYY') 
var start = contractMoment; 
var end = contractMoment; 

start = contractMoment.add(19, 'days'); 
end = contractMoment.add(51, 'days'); 

contractMoment, bevor ich wie folgt aussieht hinzu:

Thu Dec 02 2004 00:00:00 GMT-0600 (Central Standard Time) 

und nachdem ich das Hinzufügen und Konsolenprotokoll Anfang und Ende zu tun, hier ist das, was ich bekomme:

Thu Dec 02 2004 00:00:00 GMT-0600 (Central Standard Time) 

Es gibt einen Moment Objekt für jeden, was ich hier fehlt? ist das hinzugefügte Datum irgendwo im Momentobjekt vergraben?

Antwort

1

Die Methode add() kein neues Moment zurück. Es modifiziert den Moment und gibt ihn zurück. Sie müssen Kopien erstellen:

var contractMoment = moment(contract, 'DD/MM/YYYY'); 
var start = moment(contractMoment).add(19, 'days'); 
var end = moment(contractMoment).add(51, 'days'); 

Siehe http://plnkr.co/edit/PgQuFARXGUB4fxUOxEYN?p=preview für eine Demo.

+0

+1, als zusätzliche Note, die Sie auch [ 'clone()'] (https://momentjs.com/docs/#/parsing/moment-clone/) Methode verwendet werden kann. – VincenzoC