Adding months to a date in VB2005.net

Posted on 2009-04-17
Medium Priority
Last Modified: 2013-11-26
Hello All
I have a datetimepicker giving me a date, and i have  a numericupdowm giving me a number, can anyone tell me how to use the addmonth function to add the 2 together to move along the month of the datetimepicker text output

many thanks
Question by:bostonste
  • 3
  • 2
  • 2
LVL 39

Accepted Solution

abel earned 1000 total points
ID: 24167968
What about something like:

// adding the months to the selected date:
DateTime myDate = dateTimePicker1.Value;
myDate = myDate.AddMonths((int)numericUpDown1.Value);

Open in new window


Author Comment

ID: 24168305
have tried somthing similar
            Dim daterange1 As DateTime
            Dim daterange2 As DateTime
               daterange1 = DateTimePicker1.Text
                daterange2 = daterange1.monthadd(CInt(NumericUpDown.Value))

I get the feeling that the monthadd function doesnt exist in vb2005.net or im just using it in the wrong place/context
LVL 18

Assisted Solution

UnifiedIS earned 1000 total points
ID: 24168320

Dim myDate As DateTime
myDate = datetimepicker1.Value.AddMonths(numericUpDown1.Value)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 18

Expert Comment

ID: 24168334
datetimepicker1.text is not right, you need to use datetimepicker1.value and it's AddMonths not Monthadd
LVL 39

Expert Comment

ID: 24168727
@bostonste: the monthadd method does not exist. You should use the AddMonths method. The code that I showed you was tested and worked. Also note that I used DateTimePicker1.Value and not DateTimePicker1.Text. Using .Value is easier because it is already a date.

If you use IntelliSense you should be able to see immediately that your code does not work, because it gets marked by the IDE for being wrong. Typing the dot at the end of daterange1 should show you that monthadd does not exist...

-- Abel --
LVL 39

Expert Comment

ID: 24168765
Apologies, I somehow thought you were doing C#. Here's the exact same code in VB for you to use:

' adding the months to the selected date '
Dim myDate As DateTime = dateTimePicker1.Value
myDate = myDate.AddMonths(CInt(numericUpDown1.Value))
' end '

Open in new window


Author Closing Comment

ID: 31571463
thanks chaps, both answers were great

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question