我有一个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");
}
}
答案 0 :(得分:2)
尝试
DateTime timeIn = DateTime.ParseExact(textboxesIn[i].Text.PadLeft(4, '0') + dropdownIn[i].SelectedValue.Text, "hhmm tt", CultureInfo.InvariantCulture);
您必须将HH更改为hh,因为您无法将12AM解析为HH。 之后你可以在没有问题的情况下使用timeOut - time。
答案 1 :(得分:0)
在每种情况下,当你写1到11时,它默认为AM,你可以总和12 hs,但是当你写12时总是PM(AM将是00),所以:
1PM + 12 = 13
2PM + 12 = 14
...
晚上11点+12 = 23
//现在你的奇怪结果:
12:45(PM)+12 = 24:45 =&gt; 00:45但是后一天
3:00(PM)+ 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");
}