我如何在 angular2 中将 x 个月数添加到当前日期

use*_*134 3 javascript angularjs typescript

我正在尝试在 angular2 中将 x 月数添加到当前日期,但我发现这样做很困难,我能够以我想要的方式格式化当前日期,但剩下的就是添加 x 数量的数字到当月。

JS

 this.send_date=new Date();
 this.formattedDate=new Date().toISOString().slice(0,10);
 console.log(this.formattedDate); 
Run Code Online (Sandbox Code Playgroud)

例如,如果我有 5 个月到当月,日期应该更改为 2018-04-09

小智 5

如果你想要这个由 JS

你可以使用它: new Date(new Date().setMonth(new Date().getMonth() + yourMonth))

对于您的情况: new Date(new Date().setMonth(new Date().getMonth() + 5))


Pri*_*Raj 4

在 JavaScript 中使用 setMonth() 方法

export class AppComponent  {
  name = 'Angular 5';
   send_date=new Date();
   formattedDate : any;
   constructor(){
     this.send_date.setMonth(this.send_date.getMonth()+8);
     this.formattedDate=this.send_date.toISOString().slice(0,10);
 console.log(this.formattedDate); 
   }

}
Run Code Online (Sandbox Code Playgroud)