EndOFMonth Functions

  • Thread starter Thread starter coleenholley
  • Start date Start date
C

coleenholley

I've been an Access Programmer for several years, and found that the EndOfMonth function is very valuable when trying to do closing dates and the like..

Is there a function in VB.Net that does the same thing? I've been checking the help files and found numerous date functions, but not one end of month or last day of month or month end function. IS there such a function? Can some one please send me a link to where I can find this information, or better yet, an example of how it works? Thanks very much... Coleen
 
I've been an Access Programmer for several years, and found that the
EndOfMonth function is very valuable when trying to do closing dates
and the like...

Is there a function in VB.Net that does the same thing? I've been
checking the help files and found numerous date functions, but not
one end of month or last day of month or month end function. IS
there such a function? Can some one please send me a link to where I
can find this information, or better yet, an example of how it works?
Thanks very much... Coleen

I didn't find the EndOfMonth function in the Access documentation. You are
probably looking for System.DateTime.DaysInMonth:

msgbox date.daysinmonth(2004, 1)
 
Is there a function in VB.Net that does the same thing?

Basically you use the methods you have to calculate the ones you want...so
this might work:

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

You can test it like this:

Console.WriteLine(Now.ToString)
Console.WriteLine(EndOfMonth(Now, 0).ToString)
Console.WriteLine(EndOfMonth(Now, 1).ToString)
Console.WriteLine(EndOfMonth(Now, 12).ToString)

It depends a little on whether you think adding zero should be the end of
this month. I did, so that's why I add one to the month value passed to the
formula.

Tom
 
Thank you very much! I will try this. I'm looking at DatePart, DateSerial, DateAdd functions. The problem is that none of them allow you to take today() and get the end of month value using a simple function. Then you have to take into account leap year for February when writing a function to find the last day of each month with 31 days and each month with 30 days. It's was much simpler in Access - why oh why - didn't Microsoft incorporate this functionality into VB.Net? It was SO useful! Thanks Again. Coleen
 
There are a number of different calendars (depending what region of the
world you are talking about, and what era). Each calendar implements a
GetDaysInMonth method.
To use the current region's calendar, use:

Imports System.Globalization
..
..
..
CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(year, month)

To get the current month's last day, use:

Function EndOfMonth() As Integer
Dim d As Date = Date.Today();
Return CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(d.Year,
d.Month)
End Function

The DateTime type (Date type in VB) has a similar method called DaysInMonth,
but it doesn't respect different regional calendars. It hardcodes a 365 day
year, but checks for leapyear. If you are writing an app capable of being
used internationally, it's best to use the regional calendar instead.

-Rob Teixeira [MVP]

Thank you very much! I will try this. I'm looking at DatePart,
DateSerial, DateAdd functions. The problem is that none of them allow you
to take today() and get the end of month value using a simple function.
Then you have to take into account leap year for February when writing a
function to find the last day of each month with 31 days and each month with
30 days. It's was much simpler in Access - why oh why - didn't Microsoft
incorporate this functionality into VB.Net? It was SO useful! Thanks
Again. ColeenCommunity Website: http://www.dotnetjunkies.com/newsgroups/
 
Thanks Rob & Tom :-)

Between the two of you, I have my answer. I appreciate your help. I do need to know leap years; what I am doing is calculating a post-mark date based on a selected report period. If the report period is current, I need to calculate the last day of the month for the reporting period, and add 1 month to it, so the DateAdd function works great, but finding the correct last day of month was a challenge. Thanks very much for your assistance. Coleen
 
Back
Top