subtract time from date - moment js

前端 未结 6 1847
无人共我
无人共我 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:22

    There is a simple function subtract which moment library gives us to subtract time from some time. Using it is also very simple.

    moment(Date.now()).subtract(7, 'days'); // This will subtract 7 days from current time
    moment(Date.now()).subtract(3, 'd'); // This will subtract 3 days from current time
    
    //You can do this for days, years, months, hours, minutes, seconds
    //You can also subtract multiple things simulatneously
    
    //You can chain it like this.
    moment(Date.now()).subtract(3, 'd').subtract(5. 'h'); // This will subtract 3 days and 5 hours from current time
    
    //You can also use it as object literal
    moment(Date.now()).subtract({days:3, hours:5}); // This will subtract 3 days and 5 hours from current time
    

    Hope this helps!

提交回复
热议问题