-5

I have this:

 const days = Math.floor(Date.now() + 3 * (3600 * 1000 * 24))

Is this correct for 3 days??? I need it in milliseconds format

Amanda
  • 1
  • 3
  • 2
    Does this answer your question? [How to add days to Date?](https://stackoverflow.com/questions/563406/how-to-add-days-to-date). In short what is described in this thread. No you can't do it this way because it does not take into account daylight saving, so it's not always correct. See the thread for the correct solution. – Mushroomator Mar 28 '22 at 19:48
  • @Mushroomator Just curious, there are three pages of answers there, which one do you consider to be the correct solution? – James Mar 28 '22 at 19:57
  • 3 days in milliseconds: 3 (days) * 24 (hours) * 60 (minutes) * 60 (seconds) * 1000 (milliseconds) – rehnoj Mar 28 '22 at 20:00
  • 1
    @Ivar I was referring to the answers on the linked page https://stackoverflow.com/questions/563406/how-to-add-days-to-date – James Mar 28 '22 at 20:13
  • Daylight saving doesn't make difference for what I need so I can ignore it just for now – Amanda Mar 28 '22 at 20:17
  • @Amanda It makes a difference for those that answer your question. That's why it is also very useful to add context on what it exactly is that you need it for. Note that not only time is affected by daylight saving. Also the days can be affected. (Calculating it just after midnight when the daylight saving ends will cause one day too few if you discard the time.) – Ivar Mar 28 '22 at 20:22
  • @Amanda—it's more than just DST, it's also historic changes to offsets such as when the Line Islands changed from -10 to +14, or when the US goes permanently to 1 hour ahead of its current standard times in November 2023 (probably). – RobG Mar 29 '22 at 02:32
  • The use of *Math.floor* is redundant, you're dealing with integers. `Date.now() + 3*8.64e7` is sufficient for UTC days, but not local, see @Mushroomator comment – RobG Mar 29 '22 at 02:38
  • A bit strange you delete [this question with a good answer](https://stackoverflow.com/a/74434118/295783) but keep this question which is an obvious dupe and much simpler – mplungjan Nov 14 '22 at 15:38

3 Answers3

0

Try this:

var date = new Date(); // Now
date.setDate(date.getDate() + 3); // Set now + 3 days as the new date
console.log(date);
Xanik
  • 365
  • 3
  • 9
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 29 '22 at 01:51
-2

it will be simpler like this:

const now = new Date();
const threeDayLater = Math.floor(new Date(now.setDate(now.getDate() + 3)).getTime() / 1000)
  • `new Date(...).getTIme()` is redundant as *setDate* returns the time value. Why divide by 1000? The OP wants milliseconds. Code only answers aren't helpful. – RobG Mar 29 '22 at 02:35
-5

You can create a new Date out of days, and check it yourself:

const days = Math.floor(Date.now() + 3 * (3600 * 1000 * 24))
console.log(new Date(days));
James
  • 20,957
  • 5
  • 26
  • 41