Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  65] [ 4]  / answers: 1 / hits: 174075  / 10 Years ago, thu, june 26, 2014, 12:00:00

I have for instance this datetime:



01:20:00 06-26-2014


and I want to subtract a time like this:



00:03:15


after that I'd like to format the result like this:



3 hours and 15 minutes earlier.



How can I do that using moment.js ?



edit: I tried:



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

date.subtract (time);


but the result is the same as date



Thanks


More From » momentjs

 Answers
1

Moment.subtract does not support an 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 is to specify the time delta as an object:



// Assumes string is 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

[#70419] Tuesday, June 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zariahdiamondz

Total Points: 649
Total Questions: 109
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;