C# 中如何检查日期是否已过?

发布于 2024-12-11 07:46:30 字数 373 浏览 0 评论 0原文

我正在从数据库读取 cookie 过期日期(2 小时),并且我需要检查该日期是否已过。最好的方法是什么?

例如:

public bool HasExpired(DateTime now)
{
    string expires = ReadDateFromDataBase(); // output example: 21/10/2011 21:31:00
    DateTime Expires = DateTime.Parse(expires);
    return HasPassed2hoursFrom(now, Expires);
}

我正在寻找编写 .HasPassed2hoursFrom 方法的想法。

I'm reading the date expires cookie (2 hours) from database, and I need to check if this date has passed. What's the best way to do this?

For example:

public bool HasExpired(DateTime now)
{
    string expires = ReadDateFromDataBase(); // output example: 21/10/2011 21:31:00
    DateTime Expires = DateTime.Parse(expires);
    return HasPassed2hoursFrom(now, Expires);
}

I'm looking for ideas as write the .HasPassed2hoursFrom method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

羁拥 2024-12-18 07:46:30
public bool HasPassed2hoursFrom(DateTime fromDate, DateTime expireDate) 
{
    return expireDate - fromDate > TimeSpan.FromHours(2);
}
public bool HasPassed2hoursFrom(DateTime fromDate, DateTime expireDate) 
{
    return expireDate - fromDate > TimeSpan.FromHours(2);
}
时光沙漏 2024-12-18 07:46:30
bool HasPassed2hoursFrom(DateTime now, DateTime expires)
{
    return (now - expires).TotalHours >= 2;
}
bool HasPassed2hoursFrom(DateTime now, DateTime expires)
{
    return (now - expires).TotalHours >= 2;
}
泪之魂 2024-12-18 07:46:30

定期检查日期并查看是否 now.CompareTo(expires) > 0

Periodically check the date and see if now.CompareTo(expires) > 0

〗斷ホ乔殘χμё〖 2024-12-18 07:46:30
public bool HasExpired(DateTime now)
{
    string expires = ReadDateFromDataBase(); // output example: 21/10/2011 21:31:00
    DateTime Expires = DateTime.Parse(expires);
    return now.CompareTo(Expires.Add(new TimeSpan(2, 0, 0))) > 0;
}

但由于 DateTime.Now 非常快,您不需要将其作为函数参数传递...

public bool HasExpired()
{
    string expires = ReadDateFromDataBase(); // output example: 21/10/2011 21:31:00
    DateTime Expires = DateTime.Parse(expires);
    return DateTime.Now.CompareTo(Expires.Add(new TimeSpan(2, 0, 0))) > 0;
}
public bool HasExpired(DateTime now)
{
    string expires = ReadDateFromDataBase(); // output example: 21/10/2011 21:31:00
    DateTime Expires = DateTime.Parse(expires);
    return now.CompareTo(Expires.Add(new TimeSpan(2, 0, 0))) > 0;
}

But since DateTime.Now is very fast and you don't need to pass it as function parameter...

public bool HasExpired()
{
    string expires = ReadDateFromDataBase(); // output example: 21/10/2011 21:31:00
    DateTime Expires = DateTime.Parse(expires);
    return DateTime.Now.CompareTo(Expires.Add(new TimeSpan(2, 0, 0))) > 0;
}
十二 2024-12-18 07:46:30

您可以只使用运算符

boolean hasExpired = now >= Expires;

You can just use operators

boolean hasExpired = now >= Expires;
过气美图社 2024-12-18 07:46:30
private enum DateComparisonResult
    {
        Earlier = -1,
        Later = 1,
        TheSame = 0
    };

    void comapre()
    {
        DateTime Date1 = new DateTime(2020,10,1);
        DateTime Date2 = new DateTime(2010,10,1);

        DateComparisonResult comparison;
        comparison = (DateComparisonResult)Date1.CompareTo(Date2);
        MessageBox.Show(comparison.ToString());    
    }
    //Output is "later", means date1 is later than date2 

检查日期是否已过:

来源:https://msdn.microsoft.com/en-us/library/5ata5aya%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

private enum DateComparisonResult
    {
        Earlier = -1,
        Later = 1,
        TheSame = 0
    };

    void comapre()
    {
        DateTime Date1 = new DateTime(2020,10,1);
        DateTime Date2 = new DateTime(2010,10,1);

        DateComparisonResult comparison;
        comparison = (DateComparisonResult)Date1.CompareTo(Date2);
        MessageBox.Show(comparison.ToString());    
    }
    //Output is "later", means date1 is later than date2 

To check if date has passed:

Source:https://msdn.microsoft.com/en-us/library/5ata5aya%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文