简体   繁体   English

如何将DateTime数据类型与DateTime.Now进行比较

[英]How to comparing DateTime datatype to DateTime.Now

I have a variable from a datatable using the datetime datatype. 我有一个使用datetime数据类型的数据表中的变量。 This datatype is the "scheduledTime" column. 此数据类型是"scheduledTime"列。 I am trying to compare the datatime variable to the system clock which would return a difference background color on my table. 我正在尝试将datatime变量与系统时钟进行比较,这将在我的表上返回不同的背景色。 I am not getting any results with my code and I to get some guidance... Also i am not sure how to define the "scheduledTime" datetime variable in my condition 我的代码没有得到任何结果,我也得到了一些指导...另外,我不确定如何在我的情况下定义"scheduledTime"日期时间变量

Here is the condition: 条件如下:

if scheduledTime is 0-15mins late then background color red.. 如果scheduledTime0-15mins后期则背景色红..

if scheduledTime is 15min-30mins late then background color yellow.. 如果scheduledTime15min-30mins则背景颜色为黄色。

if scheduledTime is 30mins-2hours late then background color green.. 如果scheduledTime延迟30mins-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;
    }
}

In order to get the difference in minutes between two DateTime values, you can subtract the two DateTimes then call TotalMinutes on them: 为了得到两个DateTime值之间的分钟差,您可以减去两个DateTimes,然后对它们调用TotalMinutes:

double differenceInMinutes = (currentTime - scheduledTime).TotalMinutes;

You can then use differenceInMinutes for your computations accordingly: 然后,您可以相应地使用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;

UPDATED 更新

Below's a sample on how to get the DateTime value from a DataTable. 下面是有关如何从数据表获取DateTime值的示例。 Assuming that the DataTable is stored in a variable named myDataTable and you are currently on the first row (row index of 0): 假设DataTable存储在名为myDataTable的变量中,并且您当前位于第一行(行索引为0):

DateTime scheduledTime = myDataTable.Rows[0]["SCHD DTM"];

Try to operate with TimeStan object like: 尝试使用TimeStan对象进行操作,例如:

TimeSpan timeSpan = currentTime - scheduleTime; 

after that you can simply use timeSpan.TotalMinutes property or other Total... properties. 之后,您可以简单地使用timeSpan.TotalMinutes属性或其他Total...属性。

You can compare TimeSpan values directly 您可以直接比较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
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM