C# 时间比较

展开阅读全文

C#时间比较函数

02-10

下面是我写的一个方法,进行时间比较,精度到秒,因为C#的日期类型包含毫秒等,所以我只能一个一个进行比较,rn请问各位,C#有否像sql server 那样的日期函数:取日期到秒,然后进行比较的日期函数rnrn或者各位有什么好的方案也可以提出来,大家讨论一下。rn/// rn /// Compare two DateTime:if first>second return true else return falsern /// Scale:secondrn /// rn /// source DateTimern /// target DateTimern /// boolrn private bool CompareDateTime(DateTime first, DateTime second)rn rn //Yearrn if (first.Year > second.Year)rn return true;rn else if (first.Year < second.Year)rn return false;rn else if (first.Year == second.Year)rn rn //Monthrn if (first.Month > second.Month)rn return true;rn else if (first.Month < second.Month)rn return false;rn else if (first.Month == second.Month)rn rn //Dayrn if (first.Day > second.Day)rn return true;rn else if (first.Day < second.Day)rn return false;rn else if (first.Day == second.Day)rn rn //Hourrn if (first.Hour > second.Hour)rn return true;rn else if (first.Hour < second.Hour)rn return false;rn else if (first.Hour == second.Hour)rn rn //Minutern if (first.Minute > second.Minute)rn return true;rn else if (first.Minute < second.Minute)rn return false;rn else if (first.Minute == second.Minute)rn rn //Secondrn if (first.Second > second.Second)rn return true;rn else if (first.Second < second.Second)rn return false;rn else if (first.Second == second.Second)rn rn return false;rn rn rn rn rn rn rnrn return false;rn 论坛

没有更多推荐了,返回首页