0

I'm writing a notifications scheduling function and want to limit how frequently I send a user notifications, so need some Date math (e.g., nowDate > lastSend + 1 day). I'm finding JS support for Date math in posts here and here but am getting errors when trying to apply them. E.g., below attempts to add 100 minutes to user.notificationTimestamp

let whenOkayToSend: Date = user.notificationTimestamp
whenOkayToSend = whenOkayToSend + 100*60*1000

Give error

Operator '+' cannot be applied to types 'Date' and 'number'.

When I tried getMinutes and setMinutes the errors say no such functions. How do I add minutes to a Date object in cloud functions?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
buttonsrtoys
  • 2,359
  • 3
  • 32
  • 52

1 Answers1

1

You can't add a number to a Date object. If you want to roll the date forward, you can for example convert the date to a timestamp, do the math on that, and convert the timestamp back to a date:

let whenOkayToSend: Date = user.notificationTimestamp
whenOkayToSend = new Date(whenOkayToSend.getTime() + 100*60*1000)

Note that this has nothing to do with Cloud Functions, and would apply the same in any Node.js and regular JavaScript environment. So I recommend expanding your searches for that, for example: https://www.google.com/search?q=javascript+add+to+date

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807