7

Am using the following for my date in javascript

I would like to get a date plus 1 more day

so i have

new Date(2018, 1, 12, 10, 30)

THe above creates a date on 12th

Now i would like to get a date 1 week from todat

How do i go about it using Date

Geoff
  • 6,277
  • 23
  • 87
  • 197

3 Answers3

9

A very simple (but ugly) way to do it.

new Date(Date.now() + 604800000);
P Roitto
  • 1,533
  • 1
  • 11
  • 9
  • Wow amazingly works, thanks – Geoff Feb 16 '18 at 07:02
  • 2
    Not all days are 24 hours long where daylight saving is observed, so not all weeks are 6.048e8 ms long. – RobG Feb 16 '18 at 08:03
  • 1
    @RobG what do you mean not all days are 24 hours long, ive never heard of a day with less or more than 24 hours. – Geoff Feb 16 '18 at 11:49
  • 1
    That is true. This doesn't take a notice of daylight saving time. Sometimes "next week" is 1 hour earlier/later. – P Roitto Feb 16 '18 at 13:58
  • @GEOFFREYMWANGI—surely you've heard of daylight saving? The day it starts is shorter by the interval (usually 1 hour but sometimes 30 minutes), and the day it ends is longer by the same amount. Javascript Dates take daylight saving into consideration if the host system is set to observe it. – RobG Feb 18 '18 at 00:56
8

I guess you could just go:

var date = new Date(2018, 1, 12, 10, 30);
date.setDate(date.getDate() + 7);
Marty
  • 39,033
  • 19
  • 93
  • 162
3

You can follow the direction of this StackOverflow post: how to get next week date in javascript

Or you can use a library like Moment.js to make everything extremely simple for you: moment().add(7, 'days').toDate();

th3n3wguy
  • 3,649
  • 2
  • 23
  • 30
  • If this is a duplicate, you should mark it as such, not just link to the duplicate in an answer. – RobG Feb 16 '18 at 08:04
  • I don't have the reputation necessary in order to mark something as a duplicate, so this was the best thing I could do. @RobG – th3n3wguy Feb 16 '18 at 15:18