更新时间:2023-02-12 10:38:54
假设你的意思如果时间超过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.