1

I have a dateObject pulled from my database: 2021-01-02T15:12:05.000Z. I'd like to add 30 days to that date so that the end date is: 2021-02-01T15:12:05.000Z. How would I get this to work?

Reading various posts, I've tried:

let storePlanStartDate = dateObject;

storePlanStartDate.setDate(storePlanStartDate.getDate() + 30)

//returns: 1614784325000

I think the return value: 1614784325000 may be what I'm looking for. However, I'm struggling to convert it back to the same format as the original database return so that I can save the updated date.

InquisitiveTom
  • 423
  • 3
  • 13

1 Answers1

4

What you're seeing is just the default representation of a date - dates are stored as number of milliseconds since datum.

If you format the value as an ISO string (for example) you'll see it actually has the value you expected

let storePlanStartDate = new Date("2021-01-02T15:12:05.000Z");

storePlanStartDate.setDate(storePlanStartDate.getDate() + 30)

console.log(storePlanStartDate.toISOString());
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • That worked, thank you so much! – InquisitiveTom Jan 10 '22 at 16:49
  • This absolutely needs to be top answer for adding 30 days to current date javascript, was scrolling through 4 other articles looking for an explanation on how to convert that returned //returns: 1614784325000 number back into ISOString(). figures it would be a simple fix... – KingJoeffrey Jun 18 '22 at 00:50