我有一个查询事件目录的方法,并将上次密码重置的值返回到局部变量。我试图将该值与当前日期和时间进行比较,并检查它是否少于 24 小时。我想我已经接近了,但似乎无法让它发挥作用。
谢谢, 杰森
string passwordLastSet = string.Empty;
passwordLastSet = DateTime.FromFileTime((Int64)(result.Properties["PwdLastSet"][0])).ToString();
public string lastReset(DateTime pwordLastReset)
{
if (DateTime.Now.AddHours(24) <= passwordLastSet)
{
return "try again later";
}
else
{
return "all is good";
}
}
最佳答案
I am trying to compare that value to the current date and time, and check if it's been less than 24 hours.
这段代码几乎是自己写的。
DateTime timeOfLastPasswordReset = // get time of last password reset
DateTime now = DateTime.Now;
TimeSpan difference = now.Subtract(timeOfLastPasswordReset);
if(difference < TimeSpan.FromHours(24)) {
// password was reset less than twenty-four hours ago
}
else {
// password was reset no less than twenty-four hours ago
}
请注意代码的读法与您用英语指定的完全一致。
关于c# - 检查时间跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4960728/