Adding months to a date in

Posted on 2009-04-17
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
    LVL 39

    Accepted Solution

    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

    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 or im just using it in the wrong place/context
    LVL 18

    Assisted Solution


    Dim myDate As DateTime
    myDate = datetimepicker1.Value.AddMonths(numericUpDown1.Value)
    LVL 18

    Expert Comment

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

    Expert Comment

    @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

    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

    thanks chaps, both answers were great

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Response back from stored procedure 6 32
    VB.Net - Find String in Array 3 17
    Performance enhancement 39 17
    DataGridView 2 14
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now