Results 1 to 2 of 2

Thread: how to calculate the difference between two dates to get in months.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    208

    how to calculate the difference between two dates to get in months.

    I am using C# windows application
    i have two datetimepicker, startdate and enddate.
    the difference between the two datetime picker must be atleast one month.
    just the difference between the two should be months(just like one month,2 months,..3...4 ..months).
    when i select a date from the startdatepicker , the date on enddatepicker will automatically become the next month of the startdate and i can extend the enddate to other months.
    @ any time the difference between the two dates should be full months( not 1/2 month or 2.5)
    how can i do it please?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: how to calculate the difference between two dates to get in months.

    Handle the ValueChanged event of the start date control and test whether it is Focused. If it is then you should update the Value of the end date control. Get the Value of both controls, which will be DateTimes. Get the Day of the start date and use the DateTime.DaysInMonth method to determine how many days are in the month of the currently selected end date. If the first is less than or equal to the second, set the day of the end date to match the start date, otherwise set the day of the end date to the last day of that month. You can then do the same thing with the two controls reversed.

    Note that you will also need to ensure that the start date is never later then the end date.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width