subtract time from date - moment js

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

    I use moment.js http://momentjs.com/

    var start = moment(StartTimeString).toDate().getTime();
    var end = moment(EndTimeString).toDate().getTime();
    var timespan = end - start;
    var duration = moment(timespan);
    var output = duration.format("YYYY-MM-DDTHH:mm:ss");
    
    0 讨论(0)
  • 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"
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-03 03:22

    Michael Richardson's solution is great. If you would like to subtract dates (because Google will point you here if you search for it), you could also say:

    var date1 = moment( "2014-06-07 00:03:00" );
    var date2 = moment( "2014-06-07 09:22:00" );
    
    differenceInMs = date2.diff(date1); // diff yields milliseconds
    duration = moment.duration(differenceInMs); // moment.duration accepts ms
    differenceInMinutes = duration.asMinutes(); // if you would like to have the output 559
    
    0 讨论(0)
  • 2020-12-03 03:34

    You can create a much cleaner implementation with Moment.js Durations. No manual parsing necessary.

    var time = moment.duration("00:03:15");
    var date = moment("2014-06-07 09:22:06");
    date.subtract(time);
    $('#MomentRocks').text(date.format())
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.js"></script>
    <span id="MomentRocks"></span>

    0 讨论(0)
  • 2020-12-03 03:35

    I might be missing something in your question here... but from what I can gather, by using the subtract method this should be what you're looking to do:

    var timeStr = "00:03:15";
        timeStr = timeStr.split(':');
    
    var h = timeStr[1],
        m = timeStr[2];
    
    var newTime = moment("01:20:00 06-26-2014")
        .subtract({'hours': h, 'minutes': m})
        .format('hh:mm');
    
    var str = h + " hours and " + m + " minutes earlier: " + newTime;
    
    console.log(str); // 3 hours and 15 minutes earlier: 10:05
    

    $(document).ready(function(){    
         var timeStr = "00:03:15";
            timeStr = timeStr.split(':');
    
        var h = timeStr[1],
            m = timeStr[2];
    
        var newTime = moment("01:20:00 06-26-2014")
            .subtract({'hours': h, 'minutes': m})
            .format('hh:mm');
    
        var str = h + " hours and " + m + " minutes earlier: " + newTime;
    
        $('#new-time').html(str);
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
    
    
    <p id="new-time"></p>

    0 讨论(0)
提交回复
热议问题