24

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

Mark Chidlow
  • 1,432
  • 2
  • 24
  • 43
  • 2
    Dates are outside of the concern of Angular or even TypeScript. You can use the built in javascript Date object or use moment.js directly. – Igor Sep 22 '17 at 16:13
  • Is this only for display or do you want to do something else with the later date? – msanford Sep 22 '17 at 16:13
  • Thanks - yes msanford I want to use it not just for display. OK seems I'm barking up the wrong tree and I need to just use the JavaScript Date. – Mark Chidlow Sep 22 '17 at 16:14
  • `... I need to just use the JavaScript Date` <= I prefer moment.js, it's more flexible in many situations and widely supported in many other apis. This is just personal preference though. – Igor Sep 22 '17 at 16:17
  • Cool - yes I have used moment in the distant past - seems to be the defacto library for this kind of requirement. – Mark Chidlow Sep 22 '17 at 16:19
  • new Date((new Date()).getTime() + (60*60*24*1000*numberOfDays)) – Dila Gurung Aug 12 '19 at 09:01

1 Answers1

56
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

Joshua Craven
  • 4,407
  • 1
  • 31
  • 39