Tydis
Tydis

Reputation: 75

Date calculator in Visual Basic

So what I want to do is create a function that can take in input date, calculate X number of days/weeks/months later (or before) and output the resulting date. The trick being to remember to calculate leap years, months that have more or less than 30 days, etc. I'm pretty new to Visual Basic so I don't really know where to start with this. I have some ideas rolling around in my head but nothing that I can really put into code. I don't just want someone to write all the code for me, but rather give me some ideas about where to start etc.

What i'm thinking right now is to have the user click a button (btnCalc) that would prompt the user to enter the date they wish to calculate from and anther inputbox that asks how much time they wish to add/subtract from that date. These values would be passed to the CalculateDate function that would preform the calculations. I was thinking about having the user enter a negative number if they wished to get a date before the date entered and so I could have an if /else clause to determine which calculation to do based on whether the number was > or < 0. This is where I get lost. I don't know how to tell visual basic what a date is or how to decided how many days are in the months or to watch for leap years. Any help would be appreciated here, and please tell me if im going in the complete wrong direction with this. Thanks.

Upvotes: 0

Views: 2622

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241525

The DateTime class has all of these methods already on it.

result = theDate.AddDays(1)
result = theDate.AddDays(-100)
result = theDate.AddMonths(42)
result = theDate.AddYears(-10)
...etc...

See the documentation for further details.

Upvotes: 2

Related Questions