Saturday, 15 September 2012

javascript - subtract time from date - moment js -



javascript - subtract time from date - moment js -

i have instance datetime:

01:20:00 06-26-2014

and want subtract time this:

00:03:15

after i'd format result this:

3 hours , 15 minutes earlier.

how can using moment.js ?

edit: tried:

var time = moment( "00:03:15" ); var date = moment( "2014-06-07 09:22:06" ); date.subtract (time);

but result same date

thanks

moment.subtract not back upwards argument of type moment - documentation:

moment().subtract(string, number); moment().subtract(number, string); // 2.0.0 moment().subtract(string, string); // 2.7.0 moment().subtract(duration); // 1.6.0 moment().subtract(object);

the simplest solution specify time delta object:

// assumes string hh:mm:ss var mystring = "03:15:00", mystringparts = mystring.split(':'), hourdelta: +mystringparts[0], minutedelta: +mystringparts[1]; date.subtract({ hours: hourdelta, minutes: minutedelta}); date.tostring() // -> "sat jun 07 2014 06:07:06 gmt+0100"

javascript momentjs

No comments:

Post a Comment