Search code examples
angulartypescriptangular2-moment

How to add days to a date in Angular2?


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


Solution

  • 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