C#比较两个DateTime是否 [英] C# compare two DateTimes

查看:189
本文介绍了C#比较两个DateTime是否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期:

DateTime date_of_submission = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy"));
DateTime _effective_date = Convert.ToDateTime(TextBox32.Text);

现在的生效日期不能在从提交之日起的未来90天以上。

Now the effective date cannot be more than 90 days in the future from date of submission.

我怎么能这样做比较呢?

How can I do this comparison?

这所想到的一个方法是一个天真的转换日期时间为字符串,然后比较DD,MM,YYYY,看看这两个日期都在彼此的90天。但我相信,必须有比这更好的解决方案。

One method that comes to mind is a naive convert date times to strings and then compare dd, mm, yyyy and see if both dates are within 90 days of each other. But I believe there has to be a better solution than that.

推荐答案

您可以减去两个日期,并获得的时间跨度

You can subtract two dates, and get a TimeSpan :

TimeSpan difference = _effective_date - date_of_submission;
if(difference.TotalDays > 90)
{
  // Bingo!
}

这篇关于C#比较两个DateTime是否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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