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
  • 831
  • 12
  • 25
AngularM
  • 15,982
  • 28
  • 94
  • 169

3 Answers3

2

I believe this would work:

let fourWeeksFromNow = new Date();
fourWeeksFromNow.setDate(fourWeeksFromNow.getDate() + 28)
Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
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
  • 6,152
  • 2
  • 24
  • 39
  • 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
  • 14,352
  • 10
  • 69
  • 100