从日期减去时间 - 时刻js

Fra*_*ank 27 javascript momentjs

我有这个日期时间:

01:20:00 06-26-2014
Run Code Online (Sandbox Code Playgroud)

我想减去这样的时间:

00:03:15
Run Code Online (Sandbox Code Playgroud)

之后我想像这样格式化结果:

3 hours and 15 minutes earlier.

我该怎么做moment.js呢?

编辑:我试过:

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

date.subtract (time); 
Run Code Online (Sandbox Code Playgroud)

但结果是一样的 date

谢谢

Mic*_*son 31

您可以使用Moment.js 持续时间创建更清晰的实现.无需手动解析.

var time = moment.duration("00:03:15");
var date = moment("2014-06-07 09:22:06");
date.subtract(time);
$('#MomentRocks').text(date.format())
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


joe*_*ews 29

Moment.subtract不支持Moment类型的参数 - 文档:

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);
Run Code Online (Sandbox Code Playgroud)

最简单的解决方案是将时间增量指定为对象:

// 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"
Run Code Online (Sandbox Code Playgroud)


Har*_*wal 13

有一个简单的函数subtract,矩库给了我们从某个时间减去时间。使用它也非常简单。

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
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!


Mat*_*att 8

我可能在这里遗漏了你的问题...但是从我可以收集到的东西,通过使用减法方法,这应该是你想要做的:

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
Run Code Online (Sandbox Code Playgroud)

$(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);
})
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


Ice*_*ire 6

Michael Richardson的解决方案很棒.如果您想减去日期(因为如果您搜索日期,Google会将您指向此处),您还可以说:

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
Run Code Online (Sandbox Code Playgroud)