如何检查C#中的日期是否已过? [英] How to check if a date has passed in C#?

查看:129
本文介绍了如何检查C#中的日期是否已过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

例如:

 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 方法。

推荐答案

public bool HasPassed2hoursFrom(DateTime fromDate, DateTime expireDate) 
{
    return expireDate - fromDate > TimeSpan.FromHours(2);
}

这篇关于如何检查C#中的日期是否已过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆