我有一个Razor表单,其中包含以下字段:
Startdate
Starttime (24 hr)
EndDate
EndTime (24hr)
BreakDuration (increments of 15 minutes)
StartDate和Start Time EndDate和EndTime最终合并为一个SQL DateTime字段..
我需要一个表单中显示持续时间的字段 - 所以基本上是开始和结束之间的天,小时,分钟,减去休息时间。
例如:总时间:36:15小时
如果结束日期可以在开始时间和结束时间似乎超过一天时自动递增,那么也是非常有用的。例如,开始时间晚上10点 - >结束时间早上7点。
但无论如何这里是我的jsfiddle使用moment.js
http://jsfiddle.net/beebul/kwdo9yrj/
HTML:
<div class="conthttp://jsfiddle.net/beebul/kwdo9yrj/49/#forkrols">
<input type="text" style="width:300px" name="momentDurationHours" id="momentDurationHours" readonly="" />
<a href="#" id="show">Show Total duration</a>
</div>
JavaScript的:
$('#show').click(function () {
{
var startDate = '28/10/2010'; //taken from the date and time pickers
var endDate = '29/10/2010';
var startTime = '12:00';
var endTime = '17:00';
var breakduration = '01:25:00';
var joinStartDates = startDate + " " + startTime;
var joinEndDates = endDate + " " + endTime;
var totalHours = moment(joinEndDates, "DD/MM/YYYY HH:mm").diff(moment(joinStartDates, "DD/MM/YYYY HH:mm"), 'minutes');
var breaktime = moment.duration(breakduration, 'HH:mm').asMinutes();
var takeBreak = totalHours - breaktime;
//this bit below doesn't work but you get the idea...
//var totalHoursMinusBreak = moment(takeBreak).format("HH:mm"); //trying to convert mins to HH:mm
$('input[name="momentDurationHours"]').val(totalHours + " min - break: " + breaktime + "mins = TOTAL AS HRS?? ");
}
});
我试图使用时刻打结。减去功能因此决定先转换为分钟,然后尝试将最终分钟总数转换为HH:mm
非常感谢任何提示和建议。
答案 0 :(得分:2)
使用moment.js
尝试这样的事情var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
// outputs: "48:39:30"