Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date manipulation/calculation question...

Status
Not open for further replies.

bmgmzp

Programmer
Aug 18, 2006
84
US
I'm surprised that it's not easier to calculate or manipulate dates in VB .NET. For instance there is no easy way to get the first day of the previous month or the last day of next month for that matter.

So what I did was create a class that houses these funky calculations so instead of remembering these funky calculations I just call a property or method from this Calendar class. The class diagram of my class is below:

CalendarClass.jpg


I know I'm not asking a question but experience tells me that once I do something myself and then mention it... somebody tells me of a better way and/or a product that does this and more. ;-)

Thanks for letting me share.
 
This is interesting.
To get first day of previous month:
Code:
'Since every month begins with date 1:
Dim LastMonthDate As Date
LastMonthDate = Now.AddMonths(-1)
Label1.Text = Date.Parse(LastMonthDate.Month.ToString & "/1/" & LastMonthDate.Year.ToString)
Label2.Text = Date.Parse(LastMonthDate.Month.ToString & "/1/" & LastMonthDate.Year.ToString).DayOfWeek.ToString
And to get the last day of nect month:
Code:
Dim LastMonthDate As Date
LastMonthDate = Now.AddMonths(1)
Label1.Text = Date.Parse(LastMonthDate.Month.ToString & _
"/" & LastMonthDate.DaysInMonth(LastMonthDate.Year, LastMonthDate.Month) & "/" & _
LastMonthDate.Year.ToString)
Label2.Text = Date.Parse(LastMonthDate.Month.ToString & _
"/" & LastMonthDate.DaysInMonth(LastMonthDate.Year, LastMonthDate.Month) & "/" & _
LastMonthDate.Year.ToString).DayOfWeek.ToString

Adding a DataTimeStyle will handle regional settings issue.
I believe there are many other ways to do it.

Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top