简体   繁体   English

使用JavaScript或JQuery或使用moment.js计算小时,分钟-在2个日期和时间之间

[英]Calculate hour, minute in JavaScript or JQuery or with moment.js - between 2 dates and times

I have a Razor form that has the following fields: 我有一个Razor表单,其中包含以下字段:

Startdate
Starttime (24 hr)
EndDate
EndTime (24hr)
BreakDuration (increments of 15 minutes)

StartDate and Start Time an EndDate and EndTime eventually end up combined into one SQL DateTime Field.. StartDate和StartTime EndDate和EndTime最终最终合并为一个SQL DateTime字段。

I need a field in the form that displays the duration - so basically the days, hours, minutes between the start and end, minus the break duration. 我需要表单中的一个字段来显示持续时间-因此基本上从开始到结束之间的天数,小时数和分钟数减去休息时间。

for example: Total Time:36:15 hrs 例如:总时间:36:15小时

It would also be really useful if the End Date could be auto-incremented when the start time and end time seem to span over a day.. eg start-time 10pm -> end-time 7am. 如果在开始时间和结束时间似乎跨越一天的情况下可以自动增加“结束日期”,这将非常有用,例如,开始时间10pm->结束时间7am。

But anyway here is my jsfiddle using moment.js 但是无论如何这是我使用moment.js的jsfiddle

http://jsfiddle.net/beebul/kwdo9yrj/ http://jsfiddle.net/beebul/kwdo9yrj/

HTML: HTML:

    <div class="conthttp://jsfiddle.net/beebul/kwdo9yrj/49/#forkrols">
    <input type="text" style="width:300px" name="momentDurationHours" id="momentDurationHours" readonly="" />&nbsp; 
    <a href="#" id="show">Show Total duration</a>
</div>

JavaScript: 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?? ");
    }
});

I got tied in knots trying to use the moment.js subtract function so decided to convert to minutes first and then try and convert the final minutes total to HH:mm 我陷入困境,尝试使用moment.js减去函数,因此决定先转换为分钟,然后再尝试将最后的分钟总数转换为HH:mm

Many thanks for any tips and suggestions. 非常感谢您的任何提示和建议。

Try something like this using moment.js 使用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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM