subtract time from date - moment js

前端 未结 6 1823
无人共我
无人共我 2020-12-03 03:04

I have for instance this datetime:

01:20:00 06-26-2014

and I want to subtract a time like this:

00:03:15

6条回答
  •  时光取名叫无心
    2020-12-03 03:14

    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"
    

提交回复
热议问题