0
var startDate = 1582530967;

var intervalDate = moment(new Date(startDate)).add(30, 'minutes').toDate().valueOf()

console.log(intervalDate);

The above codes makes bug adding 30 minutes makes it

Monday, March 16, 2020 5:56:07 AM GMT+02:00

3 Answers3

1

If you just want to go from timestamp to a new timestamp, just add the correct number of seconds.

var startDate = 1582530967;
var intervalDate = startDate + 30 * 60; // 30 min * 60 sec in a min

If working in milliseconds:

var startDate = 1582530967000;
var intervalDate = startDate + 30 * 60 * 1000; // 1000 ms in sec
DenverCoder1
  • 2,253
  • 1
  • 10
  • 23
  • Thank you, been battling with this and searching StackOverflow without winning :) –  Feb 24 '20 at 11:24
0

I noticed that your startDate variable might be in seconds when it should be milliseconds, and therefore have 3 more digits. Here is your code with slight change to get more accurate start date:

var startDate = new Date().valueOf();
var intervalDate = moment(new Date(startDate)).add(30, 'minutes').toDate().valueOf()

Codepen link

The code seems to correctly add 30 minutes.

niiyeboah
  • 1,320
  • 9
  • 18
0

Would this solve your concern?

Working sandbox: https://codesandbox.io/s/adoring-shamir-0yuxq

//using momentjs
var moment = require("moment");

//converting timestamp to date
var time = moment.unix(1368457233).format("YYYY-MM-DD HH:mm");

//adding minutes to the date
var addingMinutes = moment(time)
  .add(3, "minutes")
  .format("YYYY MM DD");

//converting to timestamp
var afterConversion = moment(addingMinutes).format("X");

joy08
  • 9,004
  • 8
  • 38
  • 73