Kunis
Kunis

Reputation: 606

How to determine the date of an "X minutes from now" event

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:

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.

Upvotes: 1

Views: 775

Answers (3)

mm759
mm759

Reputation: 1424

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.

Upvotes: 2

Nicolas
Nicolas

Reputation: 8680

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

Upvotes: 1

baao
baao

Reputation: 73301

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>

Upvotes: 2

Related Questions