本文介绍了Javascript MomentJs错误输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1。
好的,关于第一个问题,我对Moment有疑问。它没有计算正确的时间(我认为?)
这是我的脚本:
Ok, for the first question I have a question about Moment. It doesn't calculate the time right(I think?)This is my script:
function CalculateTime() {
try {
var originalStartTime = $('#startTime').val(),
originalEndTime = $('#endTime').val(),
originalBreakTime = $('#breakTime').val();
// breaking hours and minutes. needed format: HH:mm
var startHours = originalStartTime.substring(0, 2).replace(':', ''),
startMinutes = originalStartTime.substring(3, 5),
endHours = originalEndTime.substring(0, 2).replace(':', ''),
endMinutes = originalEndTime.substring(3, 5);
// momentJs variables
var mStart = moment().hour(startHours).minute(startMinutes),
mEnd = moment().hour(endHours).minute(endMinutes),
mBreak = moment.duration(originalBreakTime);
var result = moment( mEnd - mStart - mBreak).format('HH:mm');
$('#workedHours').val(result + " timmar");
} catch (err) {
$('#workedHours').val(err);
}
startTime: 07:00,结束时间: 16:00, breakTime 00.00
这将导致 workingHours 为10.00。
这是来自chrome控制台:
startTime: 07:00, endTime: 16:00, breakTime 00.00That will result with workedHours being 10.00.This is from the chrome console:
推荐答案
您需要使用方法
function CalculateTime() {
try {
var originalStartTime = $('#startTime').val(),
originalEndTime = $('#endTime').val(),
originalBreakTime = $('#breakTime').val();
// breaking hours and minutes. needed format: HH:mm
var startHours = originalStartTime.substring(0, 2).replace(':', ''),
startMinutes = originalStartTime.substring(3, 5),
endHours = originalEndTime.substring(0, 2).replace(':', ''),
endMinutes = originalEndTime.substring(3, 5),
breakHours = originalBreakTime.substring(0, 2).replace(':', ''),
breakMinutes = originalBreakTime.substring(3, 5);
// momentJs variables
var mStart = moment().hour(startHours).minute(startMinutes),
mEnd = moment().hour(endHours).minute(endMinutes),
mBreak = moment.duration(breakHours * 60 + +breakMinutes, 'minutes');
var result = mEnd.subtract(mStart).subtract(mBreak).format('HH:mm');
$('#workedHours').val(result + " timmar");
} catch (err) {
$('#workedHours').val(err);
}
}
CalculateTime();
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="startTime" value="07:00" />
<input id="endTime" value="16:00" />
<input id="breakTime" value="00.30" />
<input id="workedHours" />
我认为更多的时刻是
function CalculateTime() {
try {
var originalStartTime = $('#startTime').val(),
originalEndTime = $('#endTime').val(),
originalBreakTime = $('#breakTime').val();
// momentJs variables
var mStart = moment(originalStartTime, 'HH:mm'),
mEnd = moment(originalEndTime, 'HH:mm'),
mBreak = moment.duration(originalBreakTime);
var result = mEnd.subtract(mStart).subtract(mBreak).format('HH:mm');
$('#workedHours').val(result + " timmar");
} catch (err) {
$('#workedHours').val(err);
}
}
CalculateTime();
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="startTime" value="07:00" />
<input id="endTime" value="16:00" />
<input id="breakTime" value="00:30" />
<input id="workedHours" />
这篇关于Javascript MomentJs错误输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!