1

I have a function that gives me a number, represents minutes. I will use minutes to trigger an up-coming event. I want to know when the event is going to be triggered. I would like to get the exact date of the trigger in a way similar to this:

  • 2016-09-06 15:47:38 (years-months-days) (hours-minutes-seconds)

I wasn't able to find a thread that relates to this exact type of getting-future-data (minutes). Can someone help me with getting this done? Would be really grateful.

Kunis
  • 576
  • 7
  • 24
  • Have you tried anything? Did you look anywhere *other* than SO? – Amit Sep 06 '16 at 16:49
  • Of course. And I have tryed. I had previously created a timer function (for a countdown timer) but in that I didn't have to convert more than minutes/seconds... – Kunis Sep 06 '16 at 16:53
  • 1
    Does this article help? http://stackoverflow.com/questions/1197928/how-to-add-30-minutes-to-a-javascript-date-object – J. Schmale Sep 06 '16 at 16:54
  • @J.Schmale yeah, quite a lot ^^ thank you – Kunis Sep 06 '16 at 16:56
  • '2016-09-06 15:47:38' has no time zone, so likely represents a different moment in time depending on the time zone it's assumed to belong to (though some timezones have the same offset as others), and hence may be a different date too. – RobG Sep 07 '16 at 04:54

3 Answers3

2

You can use momentjs and its add method:

var minutes = 28332832;
console.log(moment().add(minutes, 'minutes').format("YYYY-MM-DD HH:mm:ss"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment-with-locales.min.js"></script>
baao
  • 71,625
  • 17
  • 143
  • 203
2

A plain javascript solution that uses a timestamp as milliseconds since 1970-01-01 (the epoch):

new Date(Date.now() + minutes*60*1000).toLocaleString()

If the format is not as required you can get year, month, day and so on and build your string on your own.

An alternative solution is to use the library moment. js.

mm759
  • 1,404
  • 1
  • 9
  • 7
  • Not so much "the" timestamp but "a" timestamp. A timestamp is just something that represents a date and time e.g. '2016-02-29T23:41:016Z' is a timestamp, see [*Date and Time on the Internet: Timestamps*](https://tools.ietf.org/html/rfc3339). – RobG Sep 07 '16 at 04:52
  • It's changed, now: "the timestamp" - > "a timestamp". – mm759 Sep 07 '16 at 06:02
1

you could use momentJS to make a momentObject of your date, and then use the .add() function to add any interval.
For exemple

moment('2016-09-06 15:47:38', 'YYYY-MM-DD H:i:s').add('minutes', 5);

I'm not sure about the format in the first method but this should work.
if not, you can check on The official MomentJS Documentation for parsing format and more.
Hopes it helps !

  • Nic
Nicolas
  • 8,077
  • 4
  • 21
  • 51