Reputation: 81
How to calculate the last day of the next month using vb.net?
Upvotes: 4
Views: 33500
Reputation: 728
If you after the actual date....
You could use DateSerial(Year, Month, 0)
this will return the last day of the month prior to the month entered.
Entering DateSerial(2016, 07, 0)
will return "30/06/2016"
To get the last day of next month do DateSerial(Now.Year, Now.Month + 2, 0)
This works fine for February and also the year end as well (DateSerial(2016, 3, 0)
returns "29/02/2016", DateSerial(2016, 11 + 2, 0)
returns "31/12/2016")
Upvotes: 1
Reputation: 12748
An other way is to get the first day of the month after and substract 1 day.
Dim d As DateTime
d = DateTime.Now.AddMonths(2)
d = New DateTime(d.Year, d.Month, 1).AddDays(-1)
Upvotes: 2
Reputation: 164
try yo use DaysInMonth
Function which returns the number of days in the required month.
Example
System.DateTime.DaysInMonth(2016,May)
This will return (31), so you can get the date format in addition to this number to get last day of this month.
Upvotes: 14
Reputation: 35440
Dim N = DateTime.Now.AddMonths(1)
Dim D = New DateTime(N.Year, N.Month, DateTime.DaysInMonth(N.Year, N.Month))
D
will have the last day of next month.
Upvotes: 1