Results 1 to 5 of 5

Thread: How to get number of months b/w two dates

  1. #1

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    How to get number of months b/w two dates

    Hello everybody,

    I have two dates StartDate and EndDate. I need to get all the months between these two dates and adds them to arraylist or array etc. For exmaple, if StartDate is 01/20/2005 and EndDate is 02/15/2006 then I need to populate the array list like this.

    Jan-05
    Feb-05
    Mar-05
    ---
    ---
    ---
    Jan-06
    Feb-06

    Any Ideas ??

    Thanks.

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: How to get number of months b/w two dates

    If StartDate and EndDate are DateTime types then you can use:
    Code:
    TimeSpan difference = EndDate.Subtract(StartDate);
    It ain't quite as straight forward as it sounds though see here

    HTH

    DJ

  3. #3

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: How to get number of months b/w two dates

    I can only find minutes, seconds, days, hours etc. in the span object. How do I get a list of months starting from StartDate to EndDate.

    Thanks

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: How to get number of months b/w two dates

    That's part of the reason it's not so straight forward! Work out how many hours there are in a month!

    DJ

  5. #5

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: How to get number of months b/w two dates

    Thanks dj4uk. I have resolved the problem
    Code:
    					// Get portfolio Start Date
    					////////////////////////////////////////////////////////////////////////////////
    					int startMonth=0;
    					int startYear=0;
    					DateTime startDate=DateTime.Now;
    					sql = "SELECT Portfolio.StartDate FROM Portfolio WHERE Portfolio.ID=" + portfolioID;
    					Ds = GetData(sql);
    					startDate = Convert.ToDateTime(Ds.Tables[0].Rows[0][0]);
    					
    					startMonth = startDate.Month;
    					startYear = startDate.Year;
    					////////////////////////////////////////////////////////////////////////////////
    
    
    					// Get Maximum End Date of existing projects
    					////////////////////////////////////////////////////////////////////////////////
    					int endMonth=0;
    					int endYear=0;
    					DateTime endDate = DateTime.Now;
    					sql = "SELECT DISTINCT MAX(Project.TargetDate) FROM Project WHERE Project.FKPortfolioID=" + portfolioID;
    					Ds = GetData(sql);
    					endDate = Convert.ToDateTime(Ds.Tables[0].Rows[0][0]);
    					endMonth = endDate.Month;
    					endYear = endDate.Year;
    					////////////////////////////////////////////////////////////////////////////////
    					
    					int asciiCode=65;
    
    					System.Collections.ArrayList monthArrayList = new System.Collections.ArrayList();
    					System.Collections.ArrayList yearArrayList = new System.Collections.ArrayList();
    
    					do
    					{
    						monthArrayList.Add(startMonth);
    						yearArrayList.Add(startYear);
    
    						if(startMonth==12)
    						{
    							startMonth=1;
    							startYear++;
    						}
    						else
    						{
    							startMonth++;
    						}
    					}while(!(startMonth==endMonth && startYear==endYear));		
    					// Last Value
    					monthArrayList.Add(startMonth);
    					yearArrayList.Add(startYear);

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