c# code to find exact date difference as in vb
本问题已经有最佳答案,请猛点这里访问。
在
1 2 3 4 | Dim datTim1 As Date = #1/4/2001# Dim datTim2 As Date = #1/9/2001# Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2) Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2) |
我想用C……
您可以创建自己的datediff()函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public enum Intervals { Days, Months, Years } public static int DateDiff(Intervals eInterval, System.DateTime dtInit, System.DateTime dtEnd) { if (dtEnd < dtInit) throw new ArithmeticException("Init date should be previous to End date."); switch (eInterval) { case Intervals.Days: return (dtEnd.AddDays - dtInit).TotalDays; case Intervals.Months: return ((dtEnd.Year - dtInit.Year) * 12) + dtEnd.Month - dtInit.Month; case Intervals.Years: return dtEnd.Year - dtInit.Year; default: throw new ArgumentException("Incorrect interval code."); } } |