简体   繁体   English

为什么我不能使用 MomentJS 将年份添加到我的 Angular 应用程序中?

[英]Why I can't add years to a date using MomentJS into my Angular application?

I am working on an Angular application trying to use momentJS to add a specific number of years to a date but it seems not working.我正在开发一个 Angular 应用程序,试图使用momentJS将特定年数添加到日期,但它似乎不起作用。

This is my code:这是我的代码:

changeGuaranteeEndDate(guaranteeDuration) {
    console.log("changeGuaranteeEndDate() START");
    console.log("GUARANTEE START DATE: " + this.newAssetForm.value.guarantee_start_date);
    console.log("GUARANTEE DURATION: " + this.newAssetForm.value.guarantee_duration);
    console.log("EVENT VAL: " + guaranteeDuration);

    this.newAssetForm.value.guarantee_duration = guaranteeDuration;
    console.log("GUARANTEE DURATION: " + this.newAssetForm.value.guarantee_duration);

    let myMoment: moment.Moment = moment(this.newAssetForm.value.guarantee_start_date);
    console.log("myMoment: ", myMoment);

    let myMoment2 = myMoment;
    myMoment2.add(3, 'y')

    console.log("myMoment2: ", myMoment2);
}

So basivally I am creating the myMoment object starting from the date defined into this.newAssetForm.value.guarantee_start_date .所以基本上我正在创建myMoment object 从定义到this.newAssetForm.value.guarantee_start_date的日期开始。

Then I am trying to create a new myMoment2 object starting from myMoment and I am adding 3 years to this object. Finnally I print it然后我尝试从 myMoment 开始创建一个新的myMoment2 object并且我将 3 年添加到这个 object。最后我打印它

The problem is that in the Chrome console I obtain the following output:问题是在 Chrome 控制台中我获得了以下 output:

myMoment:  
Moment {_isAMomentObject: true, _i: Fri Jan 29 2021 13:04:29 GMT+0100 (Ora standard dell’Europa centrale), _isUTC: false, _pf: {…}, _locale: Locale, …}
_d: Mon Jan 29 2024 13:04:29 GMT+0100 (Ora standard dell’Europa centrale) {}
_i: Fri Jan 29 2021 13:04:29 GMT+0100 (Ora standard dell’Europa centrale) {}
_isAMomentObject: true
_isUTC: false
_isValid: true
_locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, ordinal: ƒ, …}
_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …}
__proto__: Object

myMoment2:  
Moment {_isAMomentObject: true, _i: Fri Jan 29 2021 13:04:29 GMT+0100 (Ora standard dell’Europa centrale), _isUTC: false, _pf: {…}, _locale: Locale, …}
_d: Mon Jan 29 2024 13:04:29 GMT+0100 (Ora standard dell’Europa centrale) {}
_i: Fri Jan 29 2021 13:04:29 GMT+0100 (Ora standard dell’Europa centrale) {}
_isAMomentObject: true
_isUTC: false
_isValid: true
_locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, ordinal: ƒ, …}
_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …}
__proto__: Object

The problems is that, as you can see in the previous output, it seems that the years was not added to the original myMoment object.问题在于,正如您在之前的 output 中看到的那样,似乎没有将年份添加到原始的myMoment object 中。

What is wrong with my code?我的代码有什么问题? What am I missing?我错过了什么? How can I correctly add years to my original myMoment object?如何正确地将年份添加到我原来的 myMoment object?

Ok, you need format() to display the value of the moment object. Basically the problem is the initial value you pass to it.好的,你需要format()来显示时刻 object 的值。基本上问题是你传递给它的初始值。 Read more about it in this answer .在这个答案中阅读更多相关信息。 So your last console log should look like this:所以你最后的控制台日志应该是这样的:

console.log("myMoment2: ", myMoment2.format());

It works for me.... you can test it clicking on Run code Snippet below.它对我有用....你可以点击下面的Run code Snippet来测试它。

You can try to use the same loaded lib of my example: src moment.min.js您可以尝试使用与我的示例相同的加载库:src moment.min.js

 function changeGuaranteeEndDate(guaranteeDuration) { console.log("changeGuaranteeEndDate() START"); console.log("GUARANTEE START DATE: " + this.newAssetForm.value.guarantee_start_date); console.log("GUARANTEE DURATION: " + this.newAssetForm.value.guarantee_duration); console.log("EVENT VAL: " + guaranteeDuration); this.newAssetForm.value.guarantee_duration = guaranteeDuration; console.log("GUARANTEE DURATION: " + this.newAssetForm.value.guarantee_duration); let myMoment = moment(this.newAssetForm.value.guarantee_start_date); console.log("myMoment: ", myMoment); let myMoment2 = myMoment; myMoment2.add(3, 'y') console.log("myMoment2: ", myMoment2); } // This is just for Testing the function... and mocking object changeGuaranteeEndDate.apply({ newAssetForm:{ value:{ guarantee_start_date:new Date(), guarantee_duration:undefined } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

I am working on an Angular application trying to use momentJS to add a specific number of years to a date but it seems not working.我正在开发一个 Angular 应用程序,试图使用momentJS将特定年数添加到日期,但它似乎不起作用。

This is my code:这是我的代码:

changeGuaranteeEndDate(guaranteeDuration) {
    console.log("changeGuaranteeEndDate() START");
    console.log("GUARANTEE START DATE: " + this.newAssetForm.value.guarantee_start_date);
    console.log("GUARANTEE DURATION: " + this.newAssetForm.value.guarantee_duration);
    console.log("EVENT VAL: " + guaranteeDuration);

    this.newAssetForm.value.guarantee_duration = guaranteeDuration;
    console.log("GUARANTEE DURATION: " + this.newAssetForm.value.guarantee_duration);

    let myMoment: moment.Moment = moment(this.newAssetForm.value.guarantee_start_date);
    console.log("myMoment: ", myMoment);

    let myMoment2 = myMoment;
    myMoment2.add(3, 'y')

    console.log("myMoment2: ", myMoment2);
}

So basivally I am creating the myMoment object starting from the date defined into this.newAssetForm.value.guarantee_start_date .所以基本上我正在创建myMoment object 从定义到this.newAssetForm.value.guarantee_start_date的日期开始。

Then I am trying to create a new myMoment2 object starting from myMoment and I am adding 3 years to this object.然后我尝试从myMoment开始创建一个新的myMoment2 object,并且我将在此 object 中添加 3 年。 Finnally I print it最后我打印出来

The problem is that in the Chrome console I obtain the following output:问题是在 Chrome 控制台中,我获得了以下 output:

myMoment:  
Moment {_isAMomentObject: true, _i: Fri Jan 29 2021 13:04:29 GMT+0100 (Ora standard dell’Europa centrale), _isUTC: false, _pf: {…}, _locale: Locale, …}
_d: Mon Jan 29 2024 13:04:29 GMT+0100 (Ora standard dell’Europa centrale) {}
_i: Fri Jan 29 2021 13:04:29 GMT+0100 (Ora standard dell’Europa centrale) {}
_isAMomentObject: true
_isUTC: false
_isValid: true
_locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, ordinal: ƒ, …}
_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …}
__proto__: Object

myMoment2:  
Moment {_isAMomentObject: true, _i: Fri Jan 29 2021 13:04:29 GMT+0100 (Ora standard dell’Europa centrale), _isUTC: false, _pf: {…}, _locale: Locale, …}
_d: Mon Jan 29 2024 13:04:29 GMT+0100 (Ora standard dell’Europa centrale) {}
_i: Fri Jan 29 2021 13:04:29 GMT+0100 (Ora standard dell’Europa centrale) {}
_isAMomentObject: true
_isUTC: false
_isValid: true
_locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, ordinal: ƒ, …}
_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …}
__proto__: Object

The problems is that, as you can see in the previous output, it seems that the years was not added to the original myMoment object.问题是,正如您在之前的 output 中看到的那样,似乎没有将年份添加到原始myMoment object 中。

What is wrong with my code?我的代码有什么问题? What am I missing?我错过了什么? How can I correctly add years to my original myMoment object?如何正确地将年份添加到我原来的 myMoment object?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM