months between two dates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karunajo
    New Member
    • May 2007
    • 37

    months between two dates

    hi,

    I want to calculate tne months between two dates.Actually i am using Asp.net2.0.Is it possible to calculate using Sql or Asp,net..I want to list the months betweeen two dates.Help me...
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    What format are your dates in?

    Comment

    • karunajo
      New Member
      • May 2007
      • 37

      #3
      Date is in the format of (MM/DD/YYYY).i want to list the months..

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        One way would be to calculate the difference in numbers and then have the month names in an arraylist. HTH.

        Comment

        • karunajo
          New Member
          • May 2007
          • 37

          #5
          Displaying month

          hi,

          I want to display the months between dates

          for ex.If i give a 2 dates such as 04/05/2006 and 04/05/2007

          the result would be April,may,june. .........March

          plz help me..very very urgent..

          Comment

          • karunajo
            New Member
            • May 2007
            • 37

            #6
            plz tel me the syntax and how to do it..

            Comment

            • kenobewan
              Recognized Expert Specialist
              • Dec 2006
              • 4871

              #7
              Multiple posting is against site rules. Please read the faqs. Thanks.

              MODERATOR

              Comment

              • kenobewan
                Recognized Expert Specialist
                • Dec 2006
                • 4871

                #8
                Suggest you have a go and report back.

                Comment

                • karunajo
                  New Member
                  • May 2007
                  • 37

                  #9
                  sorry.. I wont repeat it..

                  Comment

                  • nikpreek
                    New Member
                    • Jun 2007
                    • 22

                    #10
                    Hi,

                    There is no direct way (at least to my knowledge). Write a custome function to get the array. I think you might have months for 2 years (wht wud u do then?)
                    I'm assuming that u need it for any number of years.
                    Algorithm: (In steps)
                    1. Write a function to return string array (or list for binding).
                    2. Create private string array internally from 0 to 11 index (Jan to Dec).
                    3. Accept two dates as parameters.
                    4. Now, the logic needs to take care the way u want the difference.
                    4.1 Just take month from smaller date and loop (twice) to reach to thye month in bigger date. OR
                    4.2 Take difference between the dates as timespan, then take TotalMonths from it and loop using mod ("%") to fill whole list.

                    5. Return the array (or list)

                    If I get some more time today, I'll write some code for you.

                    Cheers,
                    Nik

                    Comment

                    • weedweaver
                      New Member
                      • Jul 2007
                      • 2

                      #11
                      private int CalculateMonthD ifference(DateT ime startDate, DateTime endDate)
                      {
                      int diff = 0;

                      while(startDate <= endDate)
                      {
                      endDate = endDate.AddMont hs(-1);
                      diff++;
                      }
                      if (diff == 0)
                      {
                      return diff;
                      }
                      else
                      {
                      return diff -1;
                      }
                      }


                      This will loop through and give the number of months difference by subtracting 1 month from the end date until it less than the start date. Ok its not elegent but it works and the number of loops is not massive (e.g. 10 years diff would be 1200 loops which on todays machines is nothing)

                      Comment

                      Working...