how to get the end date of a month

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

I passed in a DateTime object, and want to get its end date of the month. Is
there a easy way to do it?

Thanks.
 
How about something like

(new DateTime(n,1,04)).AddMonth(1).AddDay(-1);

Have not compiled this, but you get the idea.
 
I think you will need to do something like:

dim dt as DateTime
....
dim year as Integer = dt.Year
dim month as Integer = dt.Month
dim days as Integer = DateTime.DaysInMonth(year, month)
dim lastDayOfMonth as new DateTime(year, month, days)


hope this helps

dominique
 
I posted this a few weeks ago the last time it was asked :-)

Public Function EndOfMonth(ByVal d As Date, ByVal m As Integer) As Date
Return d.AddMonths(m + 1).AddDays(-d.Day)
End Function

If you send it a date and 0 for the m (months) parameter it returns the end
of the month for the date. Plus you can send it a 1, 2, 12 or whatever for
months in the future. I assume it will work with months in the past but I
can't recall if I tested that.

Tom
 
Hi Bob,

I saved this very well, it is from NAK who was active here some time ago and
we had fun when he was making it, because somebody was asking how to get the
last day of the week, but than it became the month. It is maybe not your
question, but I have saved it so long that I send it and maybe you can use
it.

:-))

The following function will tell you if it is currently the last
"monday,tuesday,wednesday.." of the month. For example. if you pass it
Saturday today (30 August 2003) it will return True.
\\\
'Made by Nick Paterman
Public Function isLastDayOfMonth(ByVal iDay As DayOfWeek) As Boolean
If (Date.Now.DayOfWeek = iDay) Then
If (Date.Now.Day < Date.DaysInMonth(Now.Year, Now.Month)) Then
Dim pIntDaysLeft As Integer = Date.DaysInMonth(Now.Year,
Now.Month) - Date.Now.Day
Return (pIntDaysLeft < 7)
Else
'IS THE LAST DAY OF THE MONTH!
Return (True)
End If
Else
'THAT ISNT TODAYS DAY
Return (False)
End If
End Function
///

I hope this helps a little bit?

Cor
 
Cor,

Interesting way to handle it... so if I want to know which day of the week
is the end of the month I call it in a loop from 1 to 7 and break when I get
a True?

I probably would have it return the LastDayOfMonth and let the client a) use
the value directly or b) run through their own local loop or case statement
without the need to keep asking the function if it guessed correctly yet.
But that's just me :-)

Tom
 
Hi Tom,

I was so happy I could send it, I saved it well just for an occassion that
it should happen. It was something Nick has been very busy with, maybe he
sees this and we see him again.

I was thinking if I would make something myself. Because the end date of the
month is the date of the first day of the next month minus 1. But I saw so
many answers already that I only did send this.

Cor
 
Bob said:
I passed in a DateTime object, and want to get its end date of the month. Is
there a easy way to do it?

Aside from the other ways people have suggested:

int lastDay = DateTime.DaysInMonth (myDate.Year, myDate.Month);
DateTime endOfMonth = myDate.AddDays(lastDay-myDate.Day);
 
Back
Top