On Sat, 15 Dec 2007 00:42:02 -0800, Mike <ablyplus@yahoo.comwrote:
Quote:
[...]
It is related on the duration of marketing campaign. That duration can
be half month, one, one and half, two months etc. User make choice
about it based on months (0.5; 1; 1.5;2;2.5 month etc.) and
"FromDate".
User makes choice how many months and days he wants to take for
campaign. He can choose from 1 to 24 months and 0 or 15 days. I have
two DateTimePicker controls and after he makes choice about duration
and "FromDate" I use Value.AddMonths and AddDays to calculate
"ToDate" and write the both values in DB. [...]
>
May be this is not a good way to achieve this problem, In that case
please suggest better apporach.
Well, maybe I'm missing something but what I would do is store the
original user selection (the start date, along with the duration in months
and a 0 or 15 day fraction), rather than computing the "ToDate" and
storing that. It's trivial to compute the "ToDate" any time you need it
from the original user input. But it is more complicated to go the other
direction. Not impossible, but certainly harder and given that the
calculations are really to be based on the user's original input, I think
it makes more sense to just store that in the database instead.
All that said, as far as your original question goes, you could use the
code you posted originally, and then add some statements to deal with the
possible half-month. Presumably if the user selected a whole-month
duration, the day-of-month (DateTime.Day) for the "FromDate" and "ToDate"
will be the same. So if they are different, you know you're dealing with
a half-month situation. In that case, it's simply a matter of comparing
the day-of-month for the start and end date; if the start day-of-month is
less than the end day-of-month, you need to add 0.5 to the total month
calculation, and if it's greater, you need to subtract 0.5.
But like I said, that's a lot more complicated code than just adding a
number of months and 0 or 15 days. Your code will be much more
maintainable IMHO if you do it the simple way.
Pete