c# - 检查时间跨度

标签 c# .net .net-3.5

我有一个查询事件目录的方法,并将上次密码重置的值返回到局部变量。我试图将该值与当前日期和时间进行比较,并检查它是否少于 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/

相关文章:

c# - 从给定线程获取 SynchronizationContext

c# - 文本框,不带重音符号的搜索(忽略重音符号)

c# - 没有从 DBNull 到可空类型的隐式转换有技术原因吗?

c# - C# 中的奇数位移位结果

c# - 如何在 .NET 中使用 Regex 获取页面元数据(标题、描述、图像),如 facebook 附加 url

c# - 使用未分配的局部变量

c# - Database First to Code First EF - 迁移键和约束名称与数据库不匹配

c# - 如何计算 WPF 中客户区的偏移量?

.net - ORA-01425 : escape character must be character string of length 1

wpf - 在一个 UI 线程上跨多个 AppDomain 运行 WPF 有什么问题?