12小时制,十进制的第12小时
本文关键字:12小时 十进制 | 更新日期: 2023-09-27 18:25:32
我有一个12小时的时钟,可以用十进制计算两次之间的差值。它在下面的循环中运行,但经过测试,我发现当我输入12:00到12:59(上午或下午)之间的时间时,它发布的时间完全错误。问题是,第12个小时是一种特殊情况,不需要添加或减去12。我如何解决这个问题,使其以十进制形式显示正确的时间?
此外,由于我发布了这篇文章,我还有另一个问题;我怎样才能容易地用十进制计算总时间?
下面是GUI的图片,这样您就可以了解我们正在处理的内容。http://imgur.com/y7JezcC
protected void CalculateButton_Click(object sender, EventArgs e)
{
//Initializing
TextBox[] textboxesIn = new TextBox[7];
TextBox[] textboxesOut = new TextBox[7];
DropDownList[] dropdownIn = new DropDownList[7];
DropDownList[] dropdownOut = new DropDownList[7];
Label[] labels = new Label[7];
//Week 1 in textboxes
textboxesIn[0] = MondayW1InTextBox;
textboxesIn[1] = TuesdayW1InTextBox;
textboxesIn[2] = WednesdayW1InTextBox;
textboxesIn[3] = ThursdayW1InTextBox;
textboxesIn[4] = FridayW1InTextBox;
textboxesIn[5] = SaturdayW1InTextBox;
textboxesIn[6] = SundayW1InTextBox;
//Week 1 out textboxes
textboxesOut[0] = MondayW1OutTextBox;
textboxesOut[1] = TuesdayW1OutTextBox;
textboxesOut[2] = WednesdayW1OutTextBox;
textboxesOut[3] = ThursdayW1OutTextBox;
textboxesOut[4] = FridayW1OutTextBox;
textboxesOut[5] = SaturdayW1OutTextBox;
textboxesOut[6] = SundayW1OutTextBox;
//Week 1 in drop down list
dropdownIn[0] = MondayW1InDropDown;
dropdownIn[1] = TuesdayW1InDropDown;
dropdownIn[2] = WednesdayW1InDropDown;
dropdownIn[3] = ThursdayW1InDropDown;
dropdownIn[4] = FridayW1InDropDown;
dropdownIn[5] = SaturdayW1InDropDown;
dropdownIn[6] = SundayW1InDropDown;
//Week 1 out drop down list
dropdownOut[0] = MondayW1OutDropDown;
dropdownOut[1] = TuesdayW1OutDropDown;
dropdownOut[2] = WednesdayW1OutDropDown;
dropdownOut[3] = ThursdayW1OutDropDown;
dropdownOut[4] = FridayW1OutDropDown;
dropdownOut[5] = SaturdayW1OutDropDown;
dropdownOut[6] = SundayW1OutDropDown;
//Week 1 labels
labels[0] = MondayW1Label;
labels[1] = TuesdayW1Label;
labels[2] = WednesdayW1Label;
labels[3] = ThursdayW1Label;
labels[4] = FridayW1Label;
labels[5] = SaturdayW1Label;
labels[6] = SundayW1Label;
for (int i = 0; i < 7; i++)
{
DateTime dt = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
string timestring = dt.ToString("h:mm");
labels[i].Text = timestring;
DateTime timeIn = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
DateTime timeOut = DateTime.ParseExact(textboxesOut[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
if (dropdownIn[i].SelectedValue == "PM")
{
timeIn = timeIn.AddHours(12);
}
if (dropdownOut[i].SelectedValue == "PM")
{
timeOut = timeOut.AddHours(12);
}
labels[i].Text = (timeOut - timeIn).TotalHours.ToString("f2");
}
}
尝试
DateTime timeIn = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0') + dropdownIn[i].SelectedValue.Text, "hhmm tt", CultureInfo.InvariantCulture);
您必须将HH更改为HH,因为您无法将12AM解析为HH。之后,您可以毫无问题地超时-超时。
在任何情况下,当你写1到11时,默认情况下都是AM,你可以求和12 hs,但当你写12时,总是PM(AM是00),所以:
下午1点+12=13
下午2点+12=14
晚上11点+12=23
//现在你奇怪的结果:
12:45(下午)+12=24:45=>00:45但后一天
下午3:00+12=15
15hs(第0天)-00:45(第1天)是您的阴性结果。
一个简单的方法是在12小时的情况下休息12小时。
for (int i = 0; i < 7; i++)
{
DateTime dt = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
string timestring = dt.ToString("h:mm");
labels[i].Text = timestring;
DateTime timeIn = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
DateTime timeOut = DateTime.ParseExact(textboxesOut[i].Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
if(timeIn.Hour == 12) timeIn = timeIn.AddHours(-12); //a easy way
if (dropdownIn[i].SelectedValue == "PM")
{
timeIn = timeIn.AddHours(12);
}
if (dropdownOut[i].SelectedValue == "PM")
{
timeOut = timeOut.AddHours(12);
}
labels[i].Text = (timeOut - timeIn).TotalHours.ToString("f2");
}