Reputation: 27
I am writing an application in c# that needs to execute a code every two or three months etc. based on what month the item was added. For example if the month was February it would be represented as an int with the value 2. Therefore, it needs to run on april(4), june(6) etc. I don't really know how to do this, any help would be really great
heres what i have so far
// month is febuary
string month = monthAsInt(date);
for(int w= month; month <= 12; w++)
{
w++;
//thats not going to work if month is greater than 0
if(?)
{
//execute
}
}
Upvotes: 0
Views: 163
Reputation: 156
This may probably be usefull. Just verify month difference is multiple of 2:
DateTime dateAdded; // Your date
DateTime currentDate = DateTime.Now;
int dateAddedMonth = dateAdded.Month;
int currentDateMonth = currentDate.Month;
int difference = dateAddedMonth - currentDateMonth;
if (difference % 2 == 0)
{
// Do your stuff
}
Upvotes: 0
Reputation: 13618
int monthJump = 2;
int month = monthAsInt(date);
for(int w= month; w <= 12; w = w + monthJump){
//Execute
}
So for this, you say that it need to work every 2 or 3 months. You simply set that value in the monthJump
variable and it does the jumps for you in the FOR
loop
I am assuming, based on the method name that this
string month = monthAsInt(date);
should be
int month = monthAsInt(date);
Upvotes: 0
Reputation:
The most elegant solution is to store DateTime
object somewhere (for example, serialize and deserialize it in a file: http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx) and compare it with current every time the application loads.
Upvotes: 1
Reputation: 62265
I wuld suggest do not invent your functions for acting with DateTime (as there are a lot of non trivial stuff out there), but simply relay to .NET library functions like:
So first construct your "start" DateTime
object, after use these functions to shift date to desired edge.
Upvotes: 0