我有一个场景,我想在前两个文本框中计算OutTime
和In first textbox
后显示员工的总时间。
这是方案,
INTIME
用户将添加In Second textbox
OUTTIME
用户将添加<tr>
<td>
<asp:Label ID="lblInTime" runat="server" Text="In Time" Visible="true"></asp:Label>
<asp:TextBox ID="txtInTime" runat="server" Width="89px"></asp:TextBox> (HH:MM)
<asp:RegularExpressionValidator ID="regIntime" runat="server" ControlToValidate="txtInTime"
ErrorMessage="Please enter time in correct format" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblOutTime" runat="server" Text="Out Time" Visible="true"></asp:Label>
<asp:TextBox ID="txtOutTime" runat="server" onkeyup="sum();" Width="89">
</asp:TextBox> (HH:MM)
<asp:RegularExpressionValidator ID="regOuttime" runat="server" ControlToValidate="txtOutTime"
ErrorMessage="Please enter time in correct format" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblTotalHr" runat="server" Text="Total Hours" Visible="true"></asp:Label>
<asp:TextBox ID="txtTotalHrs" runat="server" Width="70" ReadOnly="true" onkeyUp="TimeCalculation();">
</asp:TextBox>
</td>
</tr>
。
输入两个值后,第三个文本框将显示员工工作的总时间。
这是我尝试过的。
HTML
function TimeCalculation() {
var start = $('#txtInTime').val();
var end = $('#txtOutTime').val();
var diff = new Date(end - start);
$('#txtTotalHrs').val(diff);
}
JAVASCRIPT
{{ name }}
{% for item in inventory %}
{{ item.hardware }}
{{ item.brand }}
{% endfor %}
但问题是,我在文本框中收到错误
无效日期。
注意我的日期格式为HH:MM,格式为24小时
请建议我误解的地方
答案 0 :(得分:2)
要从结尾开始减去,您必须先将它们转换为日期。
更改您的JavaScript功能:
function TimeCalculation()
var start = $('#txtInTime').val();
var startHours = parseInt(start.split(":")[0]);
var startMins = parseInt(start.split(":")[1]);
var end = $('#txtOutTime').val();
var endHours = parseInt(end.split(":")[0]);
var endMins = parseInt(end.split(":")[1]);
var diffHours = endHours - startHours;
var diffMins = endMins - startMins;
diffHours = ("0" + diffHours).slice(-2);
diffMins = ("0" + diffMins).slice(-2);
var diff = diffHours + ":" + diffMins;
$('#txtTotalHrs').val(diff);
}