|
-
Mar 3rd, 2005, 08:13 AM
#1
Thread Starter
Frenzied Member
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.
-
Mar 3rd, 2005, 09:34 AM
#2
Frenzied Member
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
-
Mar 3rd, 2005, 11:38 PM
#3
Thread Starter
Frenzied Member
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
-
Mar 4th, 2005, 04:33 AM
#4
Frenzied Member
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
-
Mar 4th, 2005, 08:11 AM
#5
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|