我正在尝试编写一个通用函数来以自定义格式返回两个日期之间的差异,但有人报告我的代码段有错误,例如试图获取这两个日期之间的差异:
15/08/2013 - 02/09/2013
我的函数返回负值和 1 个月的差异,因为 09 大于 08,所以它不像我需要的那样工作:
1 Months, -1 Weeks, -6 Days, 0 Hours, 0 Minutes and 0 Seconds
预期的结果是这样的:
0 Months, 2 Weeks, 4 Days, 0 Hours, 0 Minutes and 0 Seconds
有人可以在某些日期帮我纠正这个错误吗?
这是我的代码:
#Region " Date Difference "
' Date Difference
'
' // By Elektro H@cker
'
' Examples :
'
' MsgBox(DateDifference(DateTime.Parse("01/03/2013"), DateTime.Parse("10/04/2013"))) ' Result: 1 Months, 1 Weeks, 2 Days, 0 Hours, 0 Minutes and 0 Seconds
' MsgBox(DateDifference(DateTime.Parse("01/01/2013 14:00:00"), DateTime.Parse("02/01/2013 15:00:30"))) ' Result: 0 Months, 0 Weeks, 1 Days, 1 Hours, 0 Minutes and 30 Seconds
Private Function DateDifference(ByVal Date1 As DateTime, ByVal Date2 As DateTime) As String
Dim MonthDiff As String, WeekDiff As String, _
DayDiff As String, HourDiff As String, _
MinuteDiff As String, SecondDiff As String
MonthDiff = Convert.ToString(DateDiff("M", Date1, Date2))
WeekDiff = Convert.ToString(DateDiff("d", Date1.AddMonths(DateDiff("M", Date1, Date2)), Date2) \ 7)
DayDiff = Convert.ToString(DateDiff("d", Date1.AddMonths(DateDiff("M", Date1, Date2)), Date2) - (WeekDiff * 7))
HourDiff = Convert.ToString(DateDiff("h", Date1.AddHours(DateDiff("h", Date1, Date2)), Date2) - (Date1.Hour - Date2.Hour))
MinuteDiff = Convert.ToString(DateDiff("n", Date1.AddMinutes(DateDiff("n", Date1, Date2)), Date2) - (Date1.Minute - Date2.Minute))
SecondDiff = Convert.ToString(DateDiff("s", Date1.AddSeconds(DateDiff("s", Date1, Date2)), Date2) - (Date1.Second - Date2.Second))
Return String.Format("{0} Months, {1} Weeks, {2} Days, {3} Hours, {4} Minutes and {5} Seconds", _
MonthDiff, WeekDiff, DayDiff, HourDiff, MinuteDiff, SecondDiff)
End Function
#End Region
更新:
我正在尝试以这种方式进行操作,但是现在如何减去周数?
私有函数 DateDifference(ByVal Date1 As DateTime, ByVal Date2 As DateTime) As String
Dim MonthDiff As String, WeekDiff As String, _
DayDiff As String, HourDiff As String, _
MinuteDiff As String, SecondDiff As String
MonthDiff = Date2.Month - Date1.Month
' WeekDiff = Date2.Month - Date1.Month
DayDiff = Date2.Day - Date1.Day
HourDiff = Date2.Hour - Date1.Hour
MinuteDiff = Date2.Minute - Date1.Minute
SecondDiff = Date2.Second - Date1.Second
' MsgBox((Date2 - Date1).ToString("dd"))
Return String.Format("{0} Months, {1} Weeks, {2} Days, {3} Hours, {4} Minutes and {5} Seconds", _
MonthDiff, WeekDiff, DayDiff, HourDiff, MinuteDiff, SecondDiff)
结束功能
更新 2:
我正在尝试使用其他方法重写它以获得日期准确性,但我完全迷失了:
Private Function DateDifference(ByVal Date1 As DateTime, ByVal Date2 As DateTime) As String
Dim DayDiff As Long = Date2.Subtract(Date1).Days
Dim HourDiff As Long = Date2.Subtract(Date1).Hours
Dim MinuteDiff As Long = Date2.Subtract(Date1).Minutes
Dim SecondDiff As Long = Date2.Subtract(Date1).Seconds
Dim MilliDiff As Long = Date2.Subtract(Date1).Milliseconds
Dim MonthDiFF As Long
Dim WeekDiFF As Long
Select Case (DayDiff Mod DateTime.DaysInMonth(Date1.Year, Date1.Month))
Case Is <= 0
MonthDiFF = 0
Case Is = 1, Is <= 28
MonthDiFF = 1
Case Is > 28
End Select
MsgBox(DayDiff Mod DateTime.DaysInMonth(Date1.Year, Date1.Month))
MsgBox(MonthDiFF)
' Return String.Format("{0} Months, {1} Weeks, {2} Days, {3} Hours, {4} Minutes and {5} Seconds", _
' MonthDiff, WeekDiff, t.Days, t.Hours, t.Minutes, t.Seconds)
End Function
更新 3:
我更接近于编写代码,但就像我说过的那样,我迷失了计算的东西:
Private Function DateDifference(ByVal Date1 As DateTime, ByVal Date2 As DateTime) As String
Dim DayDiff As Long = Date2.Subtract(Date1).Days
Dim HourDiff As Long = Date2.Subtract(Date1).Hours
Dim MinuteDiff As Long = Date2.Subtract(Date1).Minutes
Dim SecondDiff As Long = Date2.Subtract(Date1).Seconds
Dim MilliDiff As Long = Date2.Subtract(Date1).Milliseconds
Dim MonthDiFF As Long
Dim WeekDiFF As Long
For X As Short = CShort(Date1.Month) To CShort(Date2.Month)
MonthDiFF =
MsgBox(DayDiff - DateTime.DaysInMonth(Date1.Year, X))
'MsgBox(DateTime.DaysInMonth(Date1.Year, X))
Next
' MsgBox(DayDiff - DateTime.DaysInMonth(Date1.Year, Date1.Month))
' MsgBox(MonthDiFF)
' Return String.Format("{0} Months, {1} Weeks, {2} Days, {3} Hours, {4} Minutes and {5} Seconds", _
' MonthDiff, WeekDiff, t.Days, t.Hours, t.Minutes, t.Seconds)
End Function