0

For example, I have a project where the vaccination date for AstraZeneca is today (August 15, 2021) and the next dosage should be the next 4 weeks? Also, I'm going to be using Firestore to store these dates which would be used for monthly reports.

Is this the correct way to store the current date?

  console.log(new Date().toLocaleString().split(",")[0]);

Output:

8/15/2021

The expected output for the estimated 2nd dose of vaccination:

9/5/2021

Also, how can I code it in a way that the system will immediately computer the estimated date for the 2nd dose of vaccine? For sure, I know adding the days to the current date won't work since there are different months with either 30 or 31 days and also both the month and year won't get updated.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
JS3
  • 1,623
  • 3
  • 23
  • 52
  • 2
    by adding 28 days to today - the Date object does the hard work for you and will result in a valid date ... if `d` is a Date, then `d.setDate(d.getDate()+28)` will always result in the date 4 weeks from `d` - that's one thing javascript got right with dates - it's easy to manipulate them without needing to know much at all – Bravo Aug 08 '21 at 08:51
  • Please do update your question to include a [Minimal, Complete, and Reproducible Code Example](https://stackoverflow.com/help/minimal-reproducible-example) so that we can see how you are computing a date 4 weeks in advance. – Drew Reese Aug 08 '21 at 08:53
  • @DrewReese - I don't think OP is doing anything - since OP "For sure" knows how Dates work :p – Bravo Aug 08 '21 at 08:54
  • 1
    @Bravo Just standard verbiage to prompt OP to try it on their own first. – Drew Reese Aug 08 '21 at 08:58

1 Answers1

0

can you please try using days instead.

Date.prototype.addDays = function (days) {
    const date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
};

// Add 7 Days
const date = new Date('2020-12-02');

console.log(date.addDays(7));
// 2020-12-09T00:00:00.000Z

Dharman
  • 30,962
  • 25
  • 85
  • 135