[英]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.. 如果
scheduledTime
是0-15mins
后期则背景色红..
if scheduledTime
is 15min-30mins
late then background color yellow.. 如果
scheduledTime
晚15min-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"];
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.