54

I need to add 1 day to a date variable in TypeScript. May I know how can I add a day to a date field in TypeScript.

wonea
  • 4,783
  • 17
  • 86
  • 139
Joe
  • 551
  • 1
  • 4
  • 3
  • 11
    This is not a duplicate, please reopen. I need to specify a TypeScript answer. TypeScript has capabilities above and beyond JavaScript. – wonea Apr 04 '17 at 10:01
  • Using DefinitelyTyped definition for DateJS https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/datejs/datejs-tests.ts // Add 1 days to Today Date.today().add(1).days(); – wonea Apr 04 '17 at 10:01

2 Answers2

80

This is just regular JavaScript, no need for TypeScript.

yourDate = new Date(yourDate.getTime() + (1000 * 60 * 60 * 24));

1000 milliseconds in a second * 60 seconds in a minute * 60 minutes in an hour * 24 hours.

Additionally you could increment the date:

yourDate.setDate(yourDate.getDate() + 1);

The neat thing about setDate is that if your date is out-of-range for the month, it will still correctly update the date (January 32 -> February 1).

See more documentation on setDate on MDN.

Adam
  • 4,445
  • 1
  • 31
  • 49
30
 addDays(date: Date, days: number): Date {
        date.setDate(date.getDate() + days);
        return date;
    }

In your case days = 1

JustLearning
  • 3,164
  • 3
  • 35
  • 52
RK_Aus
  • 886
  • 1
  • 11
  • 18