如果日期时间大于/大于30分钟,如何执行操作 [英] How do perform an action if the datetime is more than/greater than 30 min
本文介绍了如果日期时间大于/大于30分钟,如何执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个标签,可以获得返回值作为日期时间。
i想要陈述一个条件,如果日期时间是,则查看时间并执行操作超过/大于30分钟,如下面的代码所示。
谢谢。
< b>我尝试了什么:
i have a label that get a returned value as datetime.
i want to state a condition that will look at the time and perform an action if the datetime is more than/greater than 30 min as show in the code below.
thank you.
What I have tried:
lblCompletedLastTime.Text ="Last Completed at "+ dt.Rows[0]["Last_TimeCompleted"].ToString(); // this gets the datetime from the database and returns it to the label
if (lblCompletedLastTime.Text > 30min)// if the returned datetime is greater than 30min
{
to do xxxxxx
}
推荐答案
假设你的意思如果时间超过30分钟前 ,假设您将数据库中的值存储为datetime
或datetime2
,则这样的事情应该有效:
Assuming you mean "if the time is more than 30 minutes ago", and assuming you're storing the value in the database as eitherdatetime
ordatetime2
, then something like this should work:
DateTime lastCompleted = (DateTime)dt.Rows[0]["Last_TimeCompleted"];
lblCompletedLastTime.Text = string.Format("Last Completed at {0}", lastCompleted);
if (lastCompleted.ToUniversalTime().AddMinutes(30) < DateTime.UtcNow)
{
// Perform the action...
}
存在TimeSpan
结构正是你所需要的:
There exists theTimeSpan
structure which is exactly what you need:
DateTime completedOn = dt.Rows[0]["Last_TimeCompleted"];
TimeSpan elapsed = DateTime.Now - completedOn;
if (elapsed.TotalMinutes > 30)
{
// TODO
}
如您所见,您可以减去两个 DateTime
结构并获得 TimeSpan
表征它们之间时差的结构。
亲切。
As you can see, you can subtract two DateTime
structures and get a TimeSpan
structure characterizing the time-difference between them.
Kindly.
这篇关于如果日期时间大于/大于30分钟,如何执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文