Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add days to a date in Angular2? [duplicate]

I simply want to create a date that is 3 days from now for use in a typescript angular component.

I have looked at Angular2 moment but this seems only to relate to pipes. Although I did see this article about using pipes in code, looks a bit hacky...

Maybe I'm missing something as this should be really simple?!

Thanks

like image 552
Mark Chidlow Avatar asked Sep 22 '17 16:09

Mark Chidlow


People also ask

How do you add days in date in react JS?

Adding days to current Date const current = new Date(); Now, we can add the required number of days to a current date using the combination of setDate() and getDate() methods.

How do I use DatePipe in HTML?

You have to pass the locale string as an argument to DatePipe. var ddMMyyyy = this. datePipe. transform(new Date(),"dd-MM-yyyy");


1 Answers

date: Date;

ngOnInit() {
  this.date = new Date();
  this.date.setDate( this.date.getDate() + 3 );
}

Then you can use the date pipe to display the date nicely in your HTML

{{ date | date }}

Which renders thusly:

Sep 25, 2017

like image 100
Joshua Craven Avatar answered Oct 05 '22 11:10

Joshua Craven