** **解决ASP:使用Javascript&QUOT文本框小时的时差;的onchange" [英] **SOLVED** asp:textbox hour difference with Javascript "onchange"
问题描述
我在这里需要一些帮助,我有三个< ASP:文本框>
在我的形式,我需要计算其中2之间的时间差(与设置在第三差值)。
I need some help here, I have three <asp:TextBox>
in my form and I need to calculate the time difference between 2 of them (and set the difference value on the third).
我与回发做了,它的工作很好,但我想从客户端(无需回传)做到这一点。这就是为什么我想知道是否有一种方法进行计算,并显示在JavaScript中的第三个文本框中的值。
I have done it with PostBack and it's working fine, but I want to do it from client side (no PostBack needed). That's why I want to know if there is a way to make the calculation and show the value in the third TextBox with javascript.
有些时候我需要计算2不同的日期之间的时间差。但我不能设置文本框里面的日期。
Some times I will need to calculate the time difference between 2 different dates. But I can't set the "Date" inside the TextBox.
我需要的格式为HH:MM。
The Format I need is "HH:mm".
有人能帮助我?
ASPX:
<td>
<asp:TextBox ID="TBStart1" runat="server" Width="50px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TBEnd1" runat="server" Width="50px" AutoPostBack="true"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TBDuration1" runat="server" Width="50px"></asp:TextBox>
</td>
C#:
if (IsPostBack)
{
//CHECK IF THE FIELD IS NOT BLANK. IF IT'S BLANK, THE PROCESS WILL NOT START.
if (TBEnd1.Text != "")
{
DateTime veinticuatro1 = DateTime.ParseExact("23:59", "HH:mm", CultureInfo.InvariantCulture);
DateTime unminuto1 = DateTime.ParseExact("00:01", "HH:mm", CultureInfo.InvariantCulture);
DateTime inicio1;
inicio1 = new DateTime();
inicio1 = DateTime.ParseExact(TBStart1.Text, "HH:mm", CultureInfo.InvariantCulture);
DateTime fin1;
fin1 = new DateTime();
fin1 = DateTime.ParseExact(TBEnd1.Text, "HH:mm", CultureInfo.InvariantCulture);
//CHECK IF THE END TIME IS LOWER THAN THE START TIME. THIS MEANS THAT THE INTERVAL IS BETWEEN TWO DIFFERENT DATES (EXAMPLE: 23:50 TO 01:30)
if (fin1 < inicio1)
{
TimeSpan diferencia1 = fin1.Subtract(inicio1);
DateTime duracionveintitres1 = veinticuatro1.Add(diferencia1);
DateTime duracionfinal1 = duracionveintitres1.AddMinutes(1);
string dife1 = duracionfinal1.ToString("HH:mm");
TBDuration1.Text = dife1;
TBDuration1.Focus();
}
else
{
TimeSpan diferencia1 = fin1.Subtract(inicio1);
DateTime diferenciadt1 = DateTime.ParseExact(diferencia1.ToString(), "HH:mm:ss", null);
string dife1 = diferenciadt1.ToString("HH:mm");
TBDuration1.Text = dife1;
TBDuration1.Focus();
}
}
一些字段名在西班牙语(diferencia,duracionveintitres等)。对不起这一点。
Some of the field names are in Spanish (diferencia, duracionveintitres, etc). Sorry for that.
推荐答案
最后,我找到了一个解决方案。
Finally, I found a solution for this.
我不得不改变我的&LT; asp.TextBox&GT;
到&LT;输入/&GT;
。我发现此URL中的脚本:
https://www.linuxito.com/programacion/483-como -restar-horas烯的JavaScript
I had to change my <asp.TextBox>
to <input/>
. I found the script in this URL:
https://www.linuxito.com/programacion/483-como-restar-horas-en-javascript
SCRIPT:
function HourDifference() {
start = document.getElementById("start").value;
end = document.getElementById("end").value;
startMinutes = parseInt(start.substr(3, 2));
startHours = parseInt(start.substr(0, 2));
endMinutes = parseInt(end.substr(3, 2));
endHours = parseInt(end.substr(0, 2));
minutesDiff = endMinutes - startMinutes;
hoursDiff = endHours - startHours;
if (minutesDiff < 0) {
hoursDiff --;
minutesDiff = 60 + minutesDiff;
}
if (minutesDiff < 10) {
minutesDiff = "0" + minutesDiff;
}
if (hoursDiff < 0) {
hoursDiff = 24 + hoursDiff;
}
hours = hoursDiff.toString();
minutes = minutesDiff.toString();
if (hours.length < 2) {
hours = "0" + hours;
}
if (minutes.length < 2) {
minutes = minutes + "0";
}
document.getElementById("difference").value = hours + ":" + minutes;
}
HTML
<p><input type="text" id="start" value=""/></p>
<p><input type="text" id="end" value="" onchange="HourDifference();" /></p>
<p><input type="text" id="difference" value="" /></p>
这是工作确定。
输入格式应为HH:MM。(如果凌晨1:00,是01:00;若下午1:00,是13:00)
The input format should be "HH:mm" (if 1:00 am, is 01:00; if 1:00 pm, is 13:00).
这篇关于** **解决ASP:使用Javascript&QUOT文本框小时的时差;的onchange&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!