Dillinger
Dillinger

Reputation: 1903

How to add minutes to a specific date?

I've this date:

2015-10-27 10:50:00

And in the minutes variabile I've this valorization: 30
Now for add minutes to the date initially I convert my date in the format that I want like this:

var dbdate = moment(moment(appointment['end_datetime']).format('YYYY-MM-DD HH:mm:ss'));

dbdate.add({minutes: min});

But this return the same date above. NB: the appointment ['end_datetime'] contain the date above. What am I doing wrong?

Upvotes: 0

Views: 116

Answers (3)

Shiv
Shiv

Reputation: 149

Simple JS Date() method can do the trick.

var date= new Date('2015-10-27 10:50:00');

value in date now is 'Tue Oct 27 2015 10:50:00 GMT+0530 (India Standard Time)'

date.setMinutes(date.getMinutes()+30);

and now date variable has increased datetime to 30 minutes.

Upvotes: 0

indubitablee
indubitablee

Reputation: 8206

var dbdate = moment(appointment['end_datetime']).add(30, 'minutes').format('YYYY-MM-DD HH:mm:ss')

or using your variable

var dbdate = moment(appointment['end_datetime']).add(min, 'minutes').format('YYYY-MM-DD HH:mm:ss')

reference: http://momentjs.com/docs/#/manipulating/add/

Upvotes: 2

Jamiec
Jamiec

Reputation: 136114

Dont format it first

var dbdate = moment(appointment['end_datetime']);
dbdate.add({minutes: min});
// format dbdate just before using it/displaying it

This assumes you have the value you want to add in minutes stored in a variable min. In the code above the minutes is the property name, not the value.

Upvotes: 0

Related Questions