-1

我有一个使用datetime数据类型的数据表中的变量。此数据类型是"scheduledTime"列。我正在尝试将 datatime 变量与系统时钟进行比较,这将在我的桌子上返回不同的背景颜色。我的代码没有得到任何结果,我需要一些指导......我也不确定如何"scheduledTime"在我的情况下定义 datetime 变量

这是条件:

如果晚了,则背景颜色scheduledTime0-15mins红色..

如果晚了scheduledTime15min-30mins那么背景颜色黄色..

如果晚了,则背景颜色scheduledTime30mins-2hours绿色..

//Row Rendering event
public void Row_Rendering()
{
    DateTime currentTime = DateTime.New();
    DateTime scheduledTime = "SCHD DTM"   //<--- this is the name of the column from the table

    int i = DateTime.Compare(scheduleTime,currentTime); 
    if (i <= 0.25)
    {
        Style.SelectionBackColor = Color.Red;
        ForeColor = Color.White;
    }
    else if (i > 0.25 && i <=0.5)
    {
       Style.SelectionBackColor = Color.Yellow;
       ForeColor = Color.Black;
    }
    else if (i > 0.5 && i <=2)
    {
       Style.SelectionBackColor = Color.Green;
       ForeColor = Color.White;
    }
}
4

3 回答 3

2

为了获得两个 DateTime 值之间的分钟差,您可以减去两个 DateTimes 然后在它们上调用 TotalMinutes:

double differenceInMinutes = (currentTime - scheduledTime).TotalMinutes;

然后,您可以differenceInMinutes相应地用于您的计算:

if (differenceInMinutes <= 15) then
Style.SelectionBackColor = Color.Red;
ForeColor = Color.White;

else if (differenceInMinutes > 15 & differenceInMinutes <= 30) then
Style.SelectionBackColor = Color.Yellow;
ForeColor = Color.Black;

else if (differenceInMinutes > 30 & differenceInMinutes <= 120)
Style.SelectionBackColor = Color.Green;
ForeColor = Color.White;

更新

下面是有关如何从 DataTable 中获取 DateTime 值的示例。假设 DataTable 存储在一个名为的变量中myDataTable,并且您当前位于第一行(行索引为 0):

DateTime scheduledTime = myDataTable.Rows[0]["SCHD DTM"];
于 2013-01-14T06:01:57.063 回答
0

尝试使用TimeStan对象进行操作,例如:

TimeSpan timeSpan = currentTime - scheduleTime; 

之后,您可以简单地使用timeSpan.TotalMinutes属性或其他Total...属性。

于 2013-01-14T05:58:21.670 回答
0

您可以直接比较 TimeSpan 值

            DateTime currentTime = DateTime.Now;
            DateTime scheduledTime = dataReader.GetDateTime(0);

            TimeSpan timeDifference = scheduledTime - currentTime;
            if (timeDifference <= new TimeSpan(0, 15, 0)) //less 15m
            {
                Style.SelectionBackColor = Color.Red;
                ForeColor = Color.White;
            }
            else if (timeDifference <= new TimeSpan(0, 30, 0)) //15m - 30m
            {
                Style.SelectionBackColor = Color.Yellow;
                ForeColor = Color.Black;
            }
            else if (timeDifference <= new TimeSpan(2, 0, 0))//30m - 2hr
            {
                Style.SelectionBackColor = Color.Green;
                ForeColor = Color.White;
            }
            else // > 2hr
            {
                //:todo: create styles for time difference > 2 hr
            }
于 2013-01-14T06:13:37.537 回答