473,783 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(months Apart);
}

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
Dec 14 '07 #1
5 19822
On Dec 14, 9:06 am, Mike <ablyp...@yahoo .comwrote:
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(months Apart);

}

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.MinVal ue;
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
Dec 14 '07 #2
On Fri, 14 Dec 2007 06:06:09 -0800, Mike <ab******@yahoo .comwrote:
[...]
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
Dec 14 '07 #3
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 '07 #4
On Sat, 15 Dec 2007 00:42:02 -0800, Mike <ab******@yahoo .comwrote:
[...]
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
Dec 15 '07 #5
Thanks

Mike

On Sat, 15 Dec 2007 10:50:41 -0800, "Peter Duniho"
<Np*********@nn owslpianmk.comw rote:
>On Sat, 15 Dec 2007 00:42:02 -0800, Mike <ab******@yahoo .comwrote:
>[...]
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
Dec 16 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3315
by: python | last post by:
Hi- I have a lot of monthly time series data. I need to be able to compare two dates and get the number of months that they are apart. The datetime module is a daily-frequency data type. It has a timedelta object that returns the difference in days between two dates: >>> d1 = datetime.date(2002,1,1)
26
4419
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: starttime = Date.parse("Aug 10,2003, 07:07") sdt = new Date(starttime)
1
21369
by: bradleyc | last post by:
How would you calculate the difference between two dates?
6
77857
by: Ashish Sheth | last post by:
Hi All, In C#, How can I get the difference between two dates in number of months? I tried to use the Substract method of the DateTime class and it is giving me the difference in TimeSpan,From which I can get the duration in days, hours and so.. but how can I get the difference in months? Please reply ASAP. it's urgent. -- regards, Ashish Sheth
2
2525
by: Ivan | last post by:
Hi All, I just wanna ask one simply question.. Is there anyone know how to calculate the output value from a string like that?? Dim strFormula As String = "20+30+(100+20+30)*0.1" Dim douOutput As Double douOutput = Function(strFormula)
6
3426
by: lenygold via DBMonster.com | last post by:
Here is my input table: TUE MON ----------- ----------- 2 - - 25 27 - - 48 50 - - 78
7
12049
by: orajat | last post by:
hi, how do i calculate difference in time in seconds (00:09:05 - 00:09:00 = 300) where start time being a field and the difference in time is calculated in AHT field, ie last record minus previous record. this should be a continious excercise in ms access database table
12
9145
by: denveromlp | last post by:
Hello, I'm new to Access and trying to calculate a rolling 12 month average from some time data. Each data point is a date and a measurement taken at that date. As far as I can tell, the only way to take the rolling average is to create a make-table of all the data points within the last year. Then create a query to pull out the minimum date, create a second query to pull out the maximum date, create a thrid query to pull out the...
5
1550
by: Shalini Bhalla | last post by:
i have a table in which i store a value on daily basis. so, i have 30 values for a month. Now i want to get %variation in values on daily basis. Please tell me the function to be used and how in m.access
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.