Connecting Tech Pros Worldwide Help | Site Map

C# - Calculate difference between two dates and get value in month(s)

Mike
Guest
 
Posts: n/a
#1: Dec 14 '07
I use c#, V2005

How I can get difference between two dates and get value in month(s)

I had found some solutions but it is not exactly what I need.

private static int monthDifference(DateTime startDate, DateTime
endDate)
{
int monthsApart = 12 * (startDate.Year - endDate.Year) +
startDate.Month - endDate.Month;
return Math.Abs(monthsApart);
}

on this way I can get int values.
For me is important to get value for full and half month, for e.g. 1
month and 15 days…

1 month, 1.5 month, 2, 2.5, 3, 3.5 etc. Actually, I want to get some
values from 15 days to 12 months and cover all possibilities 15 days
+15 days +15 days+

something like this: 15 days, 30, 45, 60, …etc.
Is that possible
John Duval
Guest
 
Posts: n/a
#2: Dec 14 '07

re: C# - Calculate difference between two dates and get value in month(s)


On Dec 14, 9:06 am, Mike <ablyp...@yahoo.comwrote:
Quote:
I use c#, V2005
>
How I can get difference between two dates and get value in month(s)
>
I had found some solutions but it is not exactly what I need.
>
private static int monthDifference(DateTime startDate, DateTime
endDate)
{
int monthsApart = 12 * (startDate.Year - endDate.Year) +
startDate.Month - endDate.Month;
return Math.Abs(monthsApart);
>
}
>
on this way I can get int values.
For me is important to get value for full and half month, for e.g. 1
month and 15 days...
>
1 month, 1.5 month, 2, 2.5, 3, 3.5 etc. Actually, I want to get some
values from 15 days to 12 months and cover all possibilities 15 days
+15 days +15 days+
>
something like this: 15 days, 30, 45, 60, ...etc.
Is that possible
Hi Mike,
I'm not sure this is exactly what you want (your post is a bit unclear
to me), but if you're just looking for 15-day chunks between two
DateTimes, you could just subtract them and get a TimeSpan value, for
example:

DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.MinValue;
TimeSpan ts = dt1 - dt2;

And then just look at the ts.Days property and divide by 15 (and round
up or down, depending on what you want to do). However, this looks
different from the code you posted so maybe this is not what you want.
John
Peter Duniho
Guest
 
Posts: n/a
#3: Dec 14 '07

re: C# - Calculate difference between two dates and get value in month(s)


On Fri, 14 Dec 2007 06:06:09 -0800, Mike <ablyplus@yahoo.comwrote:
Quote:
[...]
1 month, 1.5 month, 2, 2.5, 3, 3.5 etc. Actually, I want to get some
values from 15 days to 12 months and cover all possibilities 15 days
+15 days +15 days+
>
something like this: 15 days, 30, 45, 60, …etc.
Is that possible
Whatever you want, surely it is _possible_.

The main question is figuring out what you want. 60 days is not exactly
two months, so the first question is: do you really want something to do
with months? Or do you just want something rounded to the nearest 15-day
increment?

There are other questions, but you need to at least get a handle on what
it is you really want to do first. Then we can worry about the smaller
details.

Pete
christery@gmail.com
Guest
 
Posts: n/a
#4: Dec 15 '07

re: C# - Calculate difference between two dates and get value in month(s)


not en exact solution but datetime.span gives u something to work
with... but not for 2.5 months - sorta dont think it likes: jan=1,
2=feb and 1.5=jeb? or jab or fan, or ... well ;)
//CY
Peter Duniho
Guest
 
Posts: n/a
#5: Dec 15 '07

re: C# - Calculate difference between two dates and get value in month(s)


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
Mike
Guest
 
Posts: n/a
#6: Dec 16 '07

re: C# - Calculate difference between two dates and get value in month(s)


Thanks

Mike

On Sat, 15 Dec 2007 10:50:41 -0800, "Peter Duniho"
<NpOeStPeAdM@nnowslpianmk.comwrote:
Quote:
>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
Closed Thread