C# 中如何检查日期是否已过?
我正在从数据库读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
定期检查日期并查看是否
now.CompareTo(expires) > 0
Periodically check the date and see if
now.CompareTo(expires) > 0
但由于 DateTime.Now 非常快,您不需要将其作为函数参数传递...
But since DateTime.Now is very fast and you don't need to pass it as function parameter...
您可以只使用运算符
You can just use operators
检查日期是否已过:
来源:https://msdn.microsoft.com/en-us/library/5ata5aya%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
To check if date has passed:
Source:https://msdn.microsoft.com/en-us/library/5ata5aya%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396