PC Review


Reply
Thread Tools Rate Thread

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

 
 
Mike
Guest
Posts: n/a
 
      14th Dec 2007
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
 
Reply With Quote
 
 
 
 
John Duval
Guest
Posts: n/a
 
      14th Dec 2007
On Dec 14, 9:06 am, Mike <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Peter Duniho
Guest
Posts: n/a
 
      14th Dec 2007
On Fri, 14 Dec 2007 06:06:09 -0800, Mike <(E-Mail Removed)> 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
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      15th Dec 2007
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
and "FromDate" I use Value.AddMonths and AddDays to calculate
"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
please suggest better apporach.

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"
<(E-Mail Removed)> wrote:

>On Fri, 14 Dec 2007 06:06:09 -0800, Mike <(E-Mail Removed)> 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

 
Reply With Quote
 
christery@gmail.com
Guest
Posts: n/a
 
      15th Dec 2007
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
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      15th Dec 2007
On Sat, 15 Dec 2007 00:42:02 -0800, Mike <(E-Mail Removed)> 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
> 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
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      16th Dec 2007
Thanks

Mike

On Sat, 15 Dec 2007 10:50:41 -0800, "Peter Duniho"
<(E-Mail Removed)> wrote:

>On Sat, 15 Dec 2007 00:42:02 -0800, Mike <(E-Mail Removed)> 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
>> 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

 
Reply With Quote
 
New Member
Join Date: Mar 2011
Posts: 1
 
      23rd Mar 2011
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; }

dateE = dtE.AddMonths(-inc).ToString("MMyyyy");
inc = inc + 1;
}
return inc;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Row and calculate difference between two dates Jazz Microsoft Excel Programming 2 5th Oct 2009 03:36 PM
Formula needed to calculate difference between Two dates and times Abee Microsoft Excel New Users 2 18th Sep 2008 06:48 AM
how do I calculate difference between two dates and times =?Utf-8?B?UGF0Q04=?= Microsoft Excel Programming 3 8th Nov 2005 05:15 PM
Calculate difference between two dates =?Utf-8?B?VHJhaW5lcg==?= Microsoft Excel Worksheet Functions 1 11th Feb 2005 03:04 PM
calculate the difference between two dates Ian Roberts Microsoft Excel Worksheet Functions 4 25th Aug 2004 04:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:18 AM.