我需要示例代码:Date_today 和到期日期
情况:如果许可证于 2010 年 2 月 21 日到期,它将计算并显示从 date_today (即 2014 年 2 月 21 日)到从 expire_date (即 2010 年 2 月 21 日)开始的范围。
注意:输出是月份而不是年份。
像这样:日期今天:2014 年 2 月 21 日到期日期:2010 年 2 月 21 日输出:48 个月
提前致谢。
我需要示例代码:Date_today 和到期日期
情况:如果许可证于 2010 年 2 月 21 日到期,它将计算并显示从 date_today (即 2014 年 2 月 21 日)到从 expire_date (即 2010 年 2 月 21 日)开始的范围。
注意:输出是月份而不是年份。
像这样:日期今天:2014 年 2 月 21 日到期日期:2010 年 2 月 21 日输出:48 个月
提前致谢。
此代码以月份为单位返回差值:(请注意,这是绝对值。要获取非绝对值,请删除该Math.Abs()
函数。)
Public Shared Function MonthDifference(lValue As DateTime, rValue As DateTime) As Integer
Return Math.Abs((lValue.Month - rValue.Month) + 12 * (lValue.Year - rValue.Year))
End Function
用法:
Dim date1 As New Date(2010, 2, 21)
Dim date2 As Date = Date.Today
Dim differenceInMonths As Integer = MonthDifference(date2, date1)
这是一种更直接的方法,但它不考虑没有 30 天的月份。
Dim date1 As New Date(2010, 2, 21)
Dim date2 As Date = Date.Today
Dim difference As TimeSpan = date2.Subtract(date1)
Dim differenceInMonths As Integer = difference.TotalDays / 30
如果您需要以不同的度量单位(如天、分钟等)得到答案,请修改第二种方法。
更新
仅月差:
''' <summary>
''' Shows the month difference between two dates with custom string format.
''' </summary>
''' <param name="Date1">Indicates the first date to compare.</param>
''' <param name="Date2">Indicates the second date to compare.</param>
Private Function DateDifference(ByVal Date1 As DateTime,
ByVal Date2 As DateTime) As Integer
Dim MonthDiff As Integer
Do Until Date1 > Date2
Date1 = Date1.AddMonths(1)
MonthDiff += 1
Loop
Return MonthDiff - 1
End Function
用法:
Dim MonthDiff As Integer = DateDifference(DateTime.Parse("01/03/2013 00:00:00"),
DateTime.Parse("09/04/2014 01:01:01"))
MsgBox(String.Format("Month Difference {0}", CStr(MonthDiff)))
原来的
我相信您可以自定义此代码段以满足您的需求。
它返回具有差异的自定义字符串格式。
使用示例:
MsgBox(DateDifference(DateTime.Parse("01/03/2013 00:00:00"),
DateTime.Parse("09/04/2014 01:01:01"),
"{0} Year(s), {1} Month(s), {2} Week(s), {3} Day(s), {4} Hour(s), {5} Minute(s) and {6} Second(s)"
这是:
' Date Difference
' ( By Elektro )
'
''' <summary>
''' Shows the difference between two dates with custom string format.
''' </summary>
''' <param name="Date1">Indicates the first date to compare.</param>
''' <param name="Date2">Indicates the second date to compare.</param>
''' <param name="StringFormat">
''' Indicates the string format to display the difference, where:
''' {0} = Years, {1} = Months, {2} = Weeks, {3} = Days, {4} = Hours, {5} = Minutes and {6} = Seconds</param>
''' <returns>System.String.</returns>
Private Function DateDifference(ByVal Date1 As DateTime,
ByVal Date2 As DateTime,
ByVal StringFormat As String) As String
Dim Time As TimeSpan
Dim YearDiff As Integer, MonthDiff As Integer, WeekDiff As Integer
Do Until Date1 > Date2
Date1 = Date1.AddMonths(1)
MonthDiff += 1
If MonthDiff = 12 Then
YearDiff += 1
MonthDiff = 0
End If
Loop
MonthDiff -= 1
Date1 = Date1.AddMonths(-1)
Time = (Date2 - Date1)
WeekDiff = (Time.Days \ 7)
Time = (Time - TimeSpan.FromDays(WeekDiff * 7))
Return String.Format(StringFormat, YearDiff, MonthDiff, WeekDiff, Time.Days, Time.Hours, Time.Minutes, Time.Seconds)
End Function