Home Articles My Account Messages Tools Join
Tech Blog, ASP.Net, VB.Net, C#.Net, Programming Help, Help Guide
StellarPC.com | Finding the First and Last Day of a Month based on the Current Date (VB.Net)
Finding the First and Last Day of a Month based on the Current Date (VB.Net)
Written by: Evan Cummings

Finding the First and Last Day of a Month based on the Current Date

Determining the starting and ending date of any particular month based on the current date is easily acheived by making use of the built in DateTime object included with the .NET Framework. With this class, Microsoft has provided us with a very flexible and intuitive interface for manipulating date and timespans for our applications. Lets take a quick look at how we can solve this situation.


Determing the First Day
Finding the first day of a particular month is fairly straightforward, but we have to compensate for the fact that we want to look at it from a perspective of the current date. For example, I want the first day of the month that was six months ago. The trick is the DateTime object doesn't simply have a 'give me X day' capabilities, we have to logically modify the initial start day based on the current day in which we are calling:



Dim startDate As DateTime = Now.Date.AddMonths(-7).AddDays(-Now.Date.Day + 1)



To accomplish this, we first get the date that was 6 months prior:



Now.Date.AddMonths(-7)...



This bit of code takes the current date and adds -7 months, or, 6 months prior (remember we are looking at the START of the month, not the end, so it needs to be 7 rather than 6).

Now that we have the current date as of 6 months ago, we need to retrieve the first day of that month, which we can accomplish by using the current day:



.AddDays(-Now.Date.Day + 1)



Since the first part of this call returns, for example, 1/7/08, we need to negate the 8th day we are current on, thus we use AddDays(-Now...) to get us back to the start of the month. The problem is, however, subtrating 7 from 7 gives us 0, or in this case, the last day of the previous month. To negate this overcompensation of a day, lets simply add one on - (-Now.Date.Day + 1).



Determinging the Last Day

We are now halfway to our goal, which means we must determine now what the last day of the month is. This can be achieved very simply now that we have the first part of our equation:



Dim endDate As DateTime = startDate.AddMonths(1).AddDays(-1)



Breaking this down, we can utilize the startDate we determined early, and:



startDate.AddMonths(1)...



... add a month to this date. We are not done yet however. Just like we went one day too far determining the first day of the month previously, we are now going 1 day too far again. The solution is to simply negate this but subtracting that day, bringing us back to the final date we wanted:



.AddDays(-1)



Conclusion

Using this method we quickly determined the starting and ending dates based on our current date. We used 6 months for illustration, but this method can easily apply to any circumstance. The DateTime object provides many additional functions to build on this, and you can use these features and functions to approach this problem from an entirely different angle to suit your needs.

Content - .NET, VB.NET, DateTime object
New Post Next 50 | Previous 50
Post# Subject: Posted By: Time:
New Post Next 50| Previous 50