1

This is how I get todays timestamp:

var timestampNow = Math.floor(Date.now() / 1000);

I want to set timestampNow to 4 weeks from now.

My initial guess is to use Math.floor(Date.now(28) / 1000);?

esote
  • 825
  • 12
  • 25
AngularM
  • 15,466
  • 27
  • 83
  • 161

3 Answers3

2

I believe this would work:

let fourWeeksFromNow = new Date();
fourWeeksFromNow.setDate(fourWeeksFromNow.getDate() + 28)
Andrew Eisenberg
  • 27,549
  • 9
  • 90
  • 141
1

javascript isn't that great when it comes to dates, so you need to parse the date to a timestamp and modify the milliseconds. but the maths is not very hard.

var timestamp4weeks = Date.now() + (1000 * 60 * 60 * 24 * 7 * 4)
  • milliseconds => seconds === * 1000
  • seconds => minutes === * 60
  • minutes => hours === * 60
  • hours => days === * 24
  • days => weeks === * 7
synthet1c
  • 5,855
  • 2
  • 19
  • 34
  • 1
    This works well for basic date math - but there are times when honoring DST is important: "I'll see you again at the same time, in 4 weeks." Simply adding the number of milliseconds will be an hour fast/slow if the 4 weeks spans a DST shift. It can also be a day off in leap years. – user2864740 Oct 01 '16 at 17:50
  • 1
    This is not adding 28 days. Instead it is adding (1000*60*69*24*28) ms which, as noted by @user2864740, is not the same thing (e.g. daylight savings, leap years, leap seconds). – kwah Oct 01 '16 at 18:58
  • > I want to set timestampNow to 4 weeks from now. – synthet1c Oct 01 '16 at 19:00
1

Moment.js makes this easy. The following are equivalent:

var days  = moment().add(28, 'days');
var weeks = moment().add(4, 'weeks');
ThisClark
  • 13,299
  • 10
  • 63
  • 93