Hi guys, i was wondering if you could assist me with my query. I have created a 'Project Form' to allow the user to enter details for a new projct e.g name, start and end date of project ect.... What i am doing now is taking the start date and adding 7 days to it to set the end week for each week, then comparing it with the end date to see if it is earlier, the same or later. If it is ealier add this new the array list, else set this date as end date and add it to the array list. e.g If the start date was Monday 3rd Jan 2011, add 7 days = 10th Monday Jan 2010 (end week date) then check if it is ealier, the same or later than the end date. If it is earlier than add to array list and again +7 days again.
public void setWeeks() { stDate = new DateTime(); enDate = new DateTime(); DateTime[] dateArray = new DateTime[50];
endOfWeekDates = new DateTime(); sevenDays = new TimeSpan(7, 0, 0, 0); // represents 7 days endWeeksList = new ArrayList(1);
stDate = System.DateTime.Parse(startDate); // convert string date to System.DateTime representation enDate = System.DateTime.Parse(endDate);
//endOfWeekDates = stDate.Add(sevenDays);
for (int i = 0; i < dateArray.Length; i++) { if (dateArray[i].Add(sevenDays) < enDate) { endWeeksList.Add(dateArray[i]); } else if (dateArray[i].Add(sevenDays) == enDate) // if same date add to list { endOfWeekDates = enDate; endWeeksList.Add(dateArray[i]); } else if (dateArray[i].Add(sevenDays) > enDate) // if over the end date add to list { endOfWeekDates = enDate; endWeeksList.Add(dateArray[i]); } MessageBox.Show("Dates are " + endWeeksList.ToString());
}
|
Here is my updated code above. Sorry for the sloppy presentation before, i did not realise my code did not look so great.
FroglegPosted Jan 17, 2011, 12:48 PM
simon taylorPosted Jan 17, 2011, 7:50 AM
i just wanted to let you know that it worked, i did have to tweak my code ever so slightly because initially it was giving me error of Systemout.outofspace.exception.... somthing like that and since you'r example was using a dropdown list and and was not, i had to change it a bit to the following:
public void setWeeks(string startDate, string endDate)
{
stDate = new DateTime();
enDate = new DateTime();
this.stDate = System.DateTime.Parse(startDate);
this.enDate = System.DateTime.Parse(endDate);
DateTime currDate = new DateTime();
List<DateTime> endWeeksList = new List<DateTime>(1);
sevenDays = new TimeSpan(7, 0, 0, 0); // represents 7 days
while (stDate < enDate)
{
if (startDatetimepcker.Value.ToString() == enDate.ToShortDateString().ToString())
{
return;
}
//currDate = stDate;
if ((stDate = stDate + sevenDays) > enDate)
{
//currDate = enDate;
endWeeksList.Add(enDate);
}
else
{
//stDate = stDate.Add(sevenDays);
endWeeksList.Add(stDate);
}
//startDate = startDate.Add(sevenDays);
//endWeeksList.Add(startDate);
weeksCounter = weeksCounter + 1;
//startDate.Add(sevenDays);
}
but it works, thanks again.
FroglegPosted Jan 6, 2011, 7:49 PM
public void setWeeks(string startDate, string endDate)
{
DateTime stDate = new DateTime();
DateTime enDate = new DateTime();
DateTime currDate = new DateTime();
List<DateTime> dtsList = new List<DateTime>();
TimeSpan sevenDays = new TimeSpan(7, 0, 0, 0); // represents 7 days
ArrayList endWeeksList = new ArrayList();
stDate = System.DateTime.Parse(startDate); // convert string date to System.DateTime representation
enDate = System.DateTime.Parse(endDate);
if (textBox1.Text == enDate.ToShortDateString().ToString())
{
return;
}
else if (stDate < enDate)//start date earlier than end date
{
currDate = stDate;
if ((currDate = currDate + sevenDays) > enDate)
{
endWeeksList.Add(enDate.ToShortDateString());
textBox1.Text = textBox2.Text;
listBox1.Items.Add(enDate.ToShortDateString().ToString());
}
else
{
endWeeksList.Add(stDate.ToShortDateString());
stDate = stDate + sevenDays;
listBox1.Items.Add(stDate.ToShortDateString().ToString());
textBox1.Text = (stDate.ToShortDateString().ToString());
}
}
}
public void poo(string startDate, string endDate)
{
DateTime stDate = new DateTime();
DateTime enDate = new DateTime();
DateTime currDate = new DateTime();
TimeSpan sevenDays = new TimeSpan(7, 0, 0, 0); // represents 7 days
ArrayList endWeeksList = new ArrayList();
stDate = System.DateTime.Parse(startDate); // convert string date to System.DateTime representation
enDate = System.DateTime.Parse(endDate);
while (stDate < enDate)
{
if (textBox1.Text == enDate.ToShortDateString().ToString())
{
return;
}
currDate = stDate;
if ((currDate = currDate + sevenDays) > enDate)
{
endWeeksList.Add(enDate.ToShortDateString());
textBox1.Text = textBox2.Text;
listBox1.Items.Add(enDate.ToShortDateString().ToString());
}
else
{
endWeeksList.Add(stDate.ToShortDateString());
stDate = stDate + sevenDays;
listBox1.Items.Add(stDate.ToShortDateString().ToString());
}
}
}
simon taylorPosted Jan 6, 2011, 6:12 PM
13th Jan(+7 days = 20th so add end date to list instead as 20th is greater than 19th)
19th Jan
but would strangely not add 7 days but add 14 days with the code you suggested in your last post and only display only jan 20th skipping 13th Jan(hope that is clear). Any reason why it adds 14 days?
Since then i've tried to tweak it a little by doing this:
while (startDate < endDate)
{
int result = DateTime.Compare(startDate, endDate);
if ((startDate = startDate + sevenDays) > endDate)
{
endWeeksList.Add(endDate);
break;
}
else
{
startDate = startDate.Add(sevenDays);
endWeeksList.Add(startDate);
weeksCounter = weeksCounter + 1;
//startDate.Add(sevenDays);
}
}
With the updated code above, it still keeps adding 14 days after the first date. I know that i'm comparing it again, but i just wanted to see what happens. So again i tried to do something different as follows:
while (startDate < endDate)
{
int result = DateTime.Compare(startDate, endDate);
if (result > 0)
{
endWeeksList.Add(endDate);
break;
}
else
{
startDate = startDate.Add(sevenDays);
endWeeksList.Add(startDate);
weeksCounter = weeksCounter + 1;
//startDate.Add(sevenDays);
}
}
This time it shows(using same dates as above):
13th Jan
20th Jan - why does it show this??
Is there any other way for me to tweak the code a little so it does not go over my end date?
FroglegPosted Jan 4, 2011, 9:03 PM
if ((stDate = stDate + sevenDays)>enDate)
{
so if stdate later than endate add textbox2.text to endWeeksList
}
something like that - there are the basics, so see what you come up with
simon taylorPosted Jan 4, 2011, 6:23 PM
public void poo(string startDate, string endDate)
{
DateTime stDate = new DateTime();
DateTime enDate = new DateTime();
TimeSpan sevenDays = new TimeSpan(7, 0, 0, 0); // represents 7 days
ArrayList endWeeksList = new ArrayList();
stDate = System.DateTime.Parse(startDate); // convert string date to System.DateTime representation
enDate = System.DateTime.Parse(endDate);
while (stDate < enDate)
{
endWeeksList.Add(stDate.ToShortDateString());
stDate = stDate + sevenDays;
listBox1.Items.Add(stDate.ToShortDateString().ToString());
if (stDate == enDate || stDate > enDate)
{
listBox1.Items.Add(enDate.ToShortDateString().ToString());
}
}
ie. 9th jan
16th Jan
23 jan
30 Jan - adding 7 would equal to 6th Feb BUT since it is over end date being 2nd Feb, finsh of by adding the end date to list
2nd Feb - last date to be printed not 6th feb.
FroglegPosted Jan 4, 2011, 12:19 PM
simon taylorPosted Jan 4, 2011, 11:55 AM
first. i have placed the start date in a list which is of type DateTime
List<DateTime> dtsList = new List<DateTime>();
dtsList.Add(startDate);
and i changed the loop to a foreach loop as follows:
Console.WriteLine("\nCapacity: {0}", endWeeksList.Capacity);Console.WriteLine("Count: {0}", endWeeksList.Count); //MessageBox.Show("sdfs"+ endDate); //this.Close(); }
Sam HobbsPosted Jan 4, 2011, 9:25 AM
You ask "how do i set the value of dts" but I see no dts.
simon taylorPosted Jan 3, 2011, 5:27 PM
Sam HobbsPosted Jan 3, 2011, 5:18 PM
simon taylorPosted Jan 3, 2011, 4:55 PM
Add 7 to the start date which would be 10th Jan, then compare IF this value is ealier, the same or later than the end date. Since it is not, add this date ie. 10th Jan to my list. Then add 7 again to the 10th(start of new week) which will be 17th Jan and compare to see if it is ealier, the same or later than the start date. Again this is ealier than 31st Jan so add this date to the list and so on......
I have refined my code to the following:
public void setWeeks()
{
stDate = new DateTime();
enDate = new DateTime();
List<DateTime> dtsList = new List<DateTime>();
sevenDays = new TimeSpan(7, 0, 0, 0); // represents 7 days
endWeeksList = new ArrayList(1);
stDate = System.DateTime.Parse(startDate); // convert string date to System.DateTime representation
enDate = System.DateTime.Parse(endDate);
dtsList.Add(stDate); // store the start date in the list
//endOfWeekDates = stDate.Add(sevenDays);
foreach (DateTime dt in dtsList)
{
if (dt.Add(sevenDays) < enDate)
{
endWeeksList.Add(dt);
}
else if (dt.Add(sevenDays) == enDate) // if same date add to list
{
dt.Equals = enDate;
endWeeksList.Add(dt);
}
else if (dt.Add(sevenDays) > enDate) // if over the end date set dt equal to end date and then add to list
{
dt.Equals = enDate;
endWeeksList.Add(dt);
}
}
MessageBox.Show("Dates are " + endWeeksList.ToString());
In my last two IF statements, how do i set the value of dts equal to the enDate variable?
FroglegPosted Jan 3, 2011, 4:01 PM
For list use count
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
// Count with the Count property.
int c = list.Count;
MessageBox.Show(Convert.ToString(c));