本文介绍了Vb.net 添加年份然后转到该月的最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 DateAdd 函数将年份添加到我现有的日期,但我想转到该特定月份的最后一个日期.
I am using DateAdd function to add years to my existing date but I want to go to last date of that specific month.
我正在使用此代码:
Dim wr As Integer = Val(txtWar_per.Text)
Dim ins_dt As Date = dtpInstall.Value
Dim war_till As Date = DateAdd(DateInterval.Year, wr, dtpInstall.Value)
dtpWar_till.Value = war_till
输出:
dtpinstall.value= 12/1/2012
txtWar_per.text= 2
dtpWar_till.value=12/1/2014
但我希望 dtpWar_till.value 为:
31/1/2014
请早点解决我的问题..非常、非常、非常紧急.
Please resolve my problem early..It's very, very, very urgent.
推荐答案
使用闰年测试
Dim tstDate1 As Date = #2/28/2011#
Dim tstDate2 As Date = #2/29/2012#
For numYears As Integer = 1 To 5
'add years and set date to the first day of the month
Dim newD As Date = New Date(tstDate1.Year + numYears, tstDate1.Month, 1)
'then add days in month-1 to date
newD = newD.AddDays(Date.DaysInMonth(newD.Year, newD.Month) - 1)
'show it
Debug.WriteLine(newD)
newD = New Date(tstDate2.Year + numYears, tstDate2.Month, 1)
newD = newD.AddDays(Date.DaysInMonth(newD.Year, newD.Month) - 1)
Debug.WriteLine(newD)
Next
这篇关于Vb.net 添加年份然后转到该月的最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!