0

我在一个 HR 项目中,我需要为员工定义 Shift Shift 有两个用于阅读穿孔卡片的字段,Swipein 和 swipeOut。我使用两个 datetimepicker,格式为时间,showupdown 为 true 来设置刷入和刷出时间,并像这样读取

public void calculateduration()
    {
        TimeSpan swipein = dtp_toTime.Value.TimeOfDay;
        TimeSpan swipeout = dtp_FromTime.Value.TimeOfDay;
        TimeSpan duration = dtp_toTime.Value.TimeOfDay - dtp_FromTime.Value.TimeOfDay;
        MessageBox.Show(duration.ToString());

    }

在我的 SQL 服务器数据库中,我将所有这三个字段 swipein、swipeout、duration 作为 Time 数据类型,我想在其中插入这些值

为了测试,我给出了四个值

Swipein Swipeout 持续时间(显示输出)上午 8 点 下午 4 点 8 小时 >>>> 同一天的白班

下午 3 点 10 点 8 小时 >> 同一天

晚上 10 点早上 6 点(第二天)-16 小时 >>> prblm..8 必须来这里

任何想法请或任何人都可以告诉我一个想法来检查收到的持续时间是否为负数

if(duration <0)
{
duration= 24-duration;
} 
4

2 回答 2

2

您可以在减去它们之前检查这些值:

TimeSpan duration;
if (swipeout >= swipein) {
  duration = swipeout - swipein;
} else {
  duration = swipeout + new TimeSpan(1, 0, 0, 0) - swipein;
}
于 2012-09-02T10:01:32.457 回答
0

问题在于 TimeSpan 结构旨在表示时间间隔,而不是特定时间。所以在这种情况下,使用 TimeSpan 表示持续时间是有意义的,但 swipein 和 swipeout 最好用 DateTime 表示。

public void calculateduration()
{
    DateTime swipein = dtp_toTime.Value;
    DateTime swipeout = dtp_FromTime.Value;
    TimeSpan duration = swipeout - swipein;
    MessageBox.Show(duration.ToString());
}

(当然,这假设 dtp_ToTime.Value 和 dtp_FromTime.Value 是 DateTimes,但我猜它们是......)

于 2012-09-02T10:40:42.083 回答