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

Discussion in 'Microsoft C# .NET' started by Mike, Dec 14, 2007.

1. MikeGuest

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

Mike, Dec 14, 2007

2. John DuvalGuest

Re: C# - Calculate difference between two dates and get value inmonth(s)

On Dec 14, 9:06 am, Mike <> wrote:
> 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

John Duval, Dec 14, 2007

3. Peter DunihoGuest

On Fri, 14 Dec 2007 06:06:09 -0800, Mike <> wrote:

> [...]
> 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

Peter Duniho, Dec 14, 2007
4. MikeGuest

Thanks Peter, that is positive approach.

I'll try to explain in more details. I do not know how to explain that
briefly.

I have two DateTime values within DB, let call it "FromDate" and
"ToDate".

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
"ToDate" and write the both values in DB.

After that I have to take these values ("FromDate" and "ToDate" ) from
DB and calculate the price of the campaign. Price is value of campaign
for the one month. In order to calculate final price for campaign
which is based on Price and campaign duration (price x campaign
duration) I should divide price value with two for the half month of
campaign or multiply with number of months (1; 1.5;2;2.5 month etc.)
if duration is larger.

That means that I need to get difference between two dates on the way
to know is that value equal to one or two, or two and half months.
For. e.g. if duration of campaign is two and half months i should get
2.5 value after comparation of From and to dates.

May be this is not a good way to achieve this problem, In that case

I know that my explanation of problem is not clear enough and I
appologize for that, but I hope you will understand.

Thanks

Mike
On Fri, 14 Dec 2007 11:28:35 -0800, "Peter Duniho"
<> wrote:

>On Fri, 14 Dec 2007 06:06:09 -0800, Mike <> wrote:
>
>> [...]
>> 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

Mike, Dec 15, 2007
5. Guest

Re: C# - Calculate difference between two dates and get value inmonth(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

, Dec 15, 2007
6. Peter DunihoGuest

On Sat, 15 Dec 2007 00:42:02 -0800, Mike <> wrote:

> [...]
> 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
> "ToDate" and write the both values in DB. [...]
>
> May be this is not a good way to achieve this problem, In that case

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

Peter Duniho, Dec 15, 2007
7. MikeGuest

Thanks

Mike

On Sat, 15 Dec 2007 10:50:41 -0800, "Peter Duniho"
<> wrote:

>On Sat, 15 Dec 2007 00:42:02 -0800, Mike <> wrote:
>
>> [...]
>> 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
>> "ToDate" and write the both values in DB. [...]
>>
>> May be this is not a good way to achieve this problem, In that case

>
>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, Dec 16, 2007
8. imran.akhtar

Joined:
Mar 23, 2011
0
The best way of calculating exact month

private int GetMonthCount()
{
int inc = 0;
bool monthCount = true;
DateTime dtS = DateTime.Parse(dt_start.Text);
DateTime dtE = DateTime.Parse(dt_end.Text);

string dateS = dtS.ToString("MMyyyy");
string dateE = dtE.ToString("MMyyyy");

while (monthCount)
{
if (dateS.Equals(dateE))
{ monthCount = false; continue; }