我正在尝试编写一个通用函数来以自定义格式返回两个日期之间的差异,但有一个人报告我我的代码段有错误,例如试图获取这两个日期之间的差异:
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
答案 0 :(得分:1)
我认为最简单的方法是首先比较两个日期,然后根据哪个日期更大(稍后)做DateDiff。这样你就可以避免负面影响。另一种方法是采用DateDiff结果的绝对值,因为只要它是1个月之前或之后的月份就无所谓了。
答案 1 :(得分:1)
为什么不只是use DateTime.Subtract
?
DateTime date1 = new DateTime(2013, 3, 4, 12, 30, 00);
DateTime date2 = DateTime.Now;
TimeSpan diff = date2.Subtract(date1); // Subtract dates gives a time span
string s = diff.ToString("hh:mm:ss");
也可以减去TimeSpan
值:
DateTime date = DateTime.Now;
TimeSpan span = new TimeSpan(5, 12, 30, 0); // 5 Days, 12.5 hours
DateTime newDate = date.Subtract(span); // Subtract time span from date gives another date
请注意DateTime
和TimeSpan
之间的区别!
答案 2 :(得分:1)
这应该做你想要的,因为大多数TimeSpan
课程给你答案:
Public Shared Function DateDifference(ByVal Date1 As DateTime, ByVal Date2 As DateTime) As String
Dim diff As TimeSpan = Date2 - Date1
Dim months = (Date2.Month - Date1.Month) + 12 * (Date2.Year - Date1.Year)
diff -= TimeSpan.FromDays(months * 30)
Dim weeks = Math.Floor(diff.Days / 7)
diff -= TimeSpan.FromDays(weeks * 7)
Dim dayDiff = (diff.Days Mod 7).ToString()
Dim hourDiff = diff.Hours.ToString()
Dim minDiff = diff.Minutes.ToString()
Dim secDiff = diff.Seconds.ToString()
Return String.Format("{0} Months, {1} Weeks, {2} Days, {3} Hours, {4} Minutes and {5} Seconds", _
months.ToString, weeks.ToString, dayDiff, hourDiff, minDiff, secDiff)
End Function
测试:
Dim diffInfo = DateDifference(New Date(2013, 8, 16, 14, 0, 0), New Date(2013, 9, 28, 15, 2, 5))
Console.WriteLine(diffInfo) '1 Months, 1 Weeks, 6 Days, 1 Hours, 2 Minutes and 5 Seconds
答案 3 :(得分:1)
以编程方式计算月份和周数,然后对剩余部分使用TimeSpan
。月份计算很棘手,使用AddMonths
的智能行为。
Private Function DateDifference(ByVal Date1 As DateTime, ByVal Date2 As DateTime) As String
Dim MonthDiff = 0
While Date1 <= Date2
Date1 = Date1.AddMonths(1)
MonthDiff += 1
End While
MonthDiff -= 1
Date1 = Date1.AddMonths(-1)
Dim t As TimeSpan = Date2 - Date1
Dim WeekDiff As Integer = t.Days \ 7
t = t - TimeSpan.FromDays(WeekDiff * 7)
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