Reputation: 141
I'm, currently, trying to make an algorithm that compares an object's capture date (they are images) to a chosen date range from the user. The capture date of the image is currently stored as mm/yyyy.
I'm converting to integers the year and month of the capture after they have been split up and stored startMonth and startYear are the users inputted values.
I am converting to integers the year and month of the capture date, after they were split and stored as month
and year
.startMonth
and startYear
store the values entered by the users.
If it is within the the date range, then I add it to the "DisplayList" from the list that is stored.
I need it to recognise that the beginning month can be greater than the ending month's date. I'm probably missing something easy.
string month = split[0];
string year = split[1];
if (startYear <= Convert.ToInt32(year) && endYear >= Convert.ToInt32(year))
{
if (startYear == Convert.ToInt32(year) && endYear == Convert.ToInt32(year))
{
if (startMonth <= Convert.ToInt32(month) && endMonth >= Convert.ToInt32(month))
{
DisplayList.Add(ImageList[i]); // Adds it to the DisplayList
}
}
else if (startYear == Convert.ToInt32(year) || endYear == Convert.ToInt32(year))
{
if (startMonth <= Convert.ToInt32(month) && endMonth >= Convert.ToInt32(month))
{
DisplayList.Add(ImageList[i]);
}
else if (startYear == Convert.ToInt32(year) && startMonth <= Convert.ToInt32(month))
{
DisplayList.Add(ImageList[i]);
}
else if (endYear == Convert.ToInt32(year) && startMonth >= Convert.ToInt32(month))
{
DisplayList.Add(ImageList[i]);
}
}
}
Upvotes: 0
Views: 72
Reputation: 4515
Having stored your dates as dates would have been simpler, but if you do not have the choice, the following should make it:
var startDate = new DateTime(Convert.ToInt32(startYear), Convert.ToInt32(startMonth), 1);
var endDate = new DateTime(Convert.ToInt32(endYear), Convert.ToInt32(endMonth), 1);
var date = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), 1);
// do whatever comparisons with date, startDate and endDate
if(startDate <= date && date <= endDate)
{
}
Also note that having many if
s to do DisplayList.Add(ImageList[i]);
is quite odd.
I would try to factor it: when exactly do I need to add an image to that display list?
And then use one if
:
if(thatCondition ||
thisCondition ||
thatOtherCondition)
{
DisplayList.Add(ImageList[i]);
}
Upvotes: 3