且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果日期时间大于/大于30分钟,如何执行操作

更新时间: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 either datetime or datetime2, 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 the TimeSpan 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.