473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

difference between two dates in Month

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
Nov 16 '05 #1
6 77780
"Ashish Sheth" <ea*****@gmail.com> wrote in
news:#D*************@tk2msftngp13.phx.gbl:
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.


Please explain how the difference in number of months can safely be
calculated from a TimeSpan value.

For instance, subtracting the last day of one month from the last day of
another month would for some be considered 1 month, but if the first date
was the 31st of january and the second date was the 28th of february, only
28 days have elapsed, is that a month ? How about from the 28th of february
to the 31st of march, that's 31 days. Is that one month too ? Even though
the second month there is bigger than the first ?

If you divide by 30 you get "1 year = 12 + 1/6 months". If you divide by 31
you get "1 year = 11 + 24/31 months". If you start with clever logic to
take the day of the month into account, you get months of varying sizes,
and TimeSpan has lost all references to the original dates so it can't
assume anything about the number of days it has stored.

The reason for years and months not appearing in the timespan class is
because of this problem. If you can define how to calculate the number of
months between two dates, just make a helper class that does just that.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #2
Hi Ashish,

A TimeSpan represents only a time interval. Since the number of days in a
month is not constant, the TimeSpan structure doesn't have a value for diff
in months.

Days/30 will work though, assuming the number of days in a month is 30.

Eg:
DateTime firstDate = new DateTime(2004, 01, 20);
DateTime secondDate = new DateTime(2004, 04, 15);
TimeSpan diff = secondDate.Subtract(firstDate);
Console.WriteLine(diff.Days/30);

Will this do?

HTH,
Rakesh Rajan

"Ashish Sheth" wrote:
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

Nov 16 '05 #3
Ashish,
In addition to the other comments, I would consider simply subtracting
DateTime.Months form DateTime.Months and adding that to DateTime.Years -
DateTime.Years multiplied by 12.

Something like (untested):

DateTime d1, d2;
int months = d1.Months - d2.Months;
int years = d1.Years - d2.Years;
int months += years * 12;

Hope this helps
Jay

"Ashish Sheth" <ea*****@gmail.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #4
Hi Jay - This won't work becaues what if the month of d1 is 01 and the month
of d2 is 02?

"Jay B. Harlow [MVP - Outlook]" wrote:
Ashish,
In addition to the other comments, I would consider simply subtracting
DateTime.Months form DateTime.Months and adding that to DateTime.Years -
DateTime.Years multiplied by 12.

Something like (untested):

DateTime d1, d2;
int months = d1.Months - d2.Months;
int years = d1.Years - d2.Years;
int months += years * 12;

Hope this helps
Jay

"Ashish Sheth" <ea*****@gmail.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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


Nov 16 '05 #5
Gwenda,
Hi Jay - This won't work becaues what if the month of d1 is 01 and the
month
of d2 is 02? Why not?

You do realize that you can either use System.Math.Abs to ensure the result
is positive or you can use a comparison & swap d1 for d2 to ensure that d1
is larger.

Of course you may want to have it return a negative just like
DateTime.Subtract does, which was what my sample shows!

Where it can be perceived it doesn't work is if you have Jan 31st & Feb 1st,
There is only a single day between them, however the algorithm I gave will
give a full month. Giving a full month may or may not be a real problem...
Hence my comment "I would consider simply"...

Another perceived problem is if you have Jan 1st & Jan 31st, is that
considered 1 month (its 30 days) or 0 months?

Obviously Ashish needs to better define what he is looking for...

Hope this helps
Jay

"gwenda" <ni**@community.nospam> wrote in message
news:4D**********************************@microsof t.com... Hi Jay - This won't work becaues what if the month of d1 is 01 and the
month
of d2 is 02?

"Jay B. Harlow [MVP - Outlook]" wrote:
Ashish,
In addition to the other comments, I would consider simply subtracting
DateTime.Months form DateTime.Months and adding that to DateTime.Years -
DateTime.Years multiplied by 12.

Something like (untested):

DateTime d1, d2;
int months = d1.Months - d2.Months;
int years = d1.Years - d2.Years;
int months += years * 12;

Hope this helps
Jay

"Ashish Sheth" <ea*****@gmail.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
> 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
>
>


Nov 16 '05 #6
Jrod
1
I ran into the same problem (which is how I found this thread) and, after googling and finding no real help, I came up with my own solution. Here is the exact code, including my overly-verbose comments, I used:

// first, we need to get the age as a TimeSpan
TimeSpan age = dateOfBirth - (new DateTime(1, 1, 1));
// then we can subtract that TimeSpan from the current date to return a DateTime object
// note that subtracting a DateTime from a DateTime returns a TimeSpan object, which is
// the reason for the first step above (we need a DateTime to get the years without
// having to go through some ugly calculation)
DateTime difference = DateTime.Now.Subtract(age);
// we'll need to subtract a year to account for that fact that we were forced to use the
// year 1 above in the new DateTime statement (throws an exception if you use 0)
sAge = difference.AddYears(-1).Year.ToString()

Obviously, to get the number of months requires only a slight modification. Something like:

int numMonths = difference.Month + (difference.AddYears(-1).Year * 12);

Hope that helps and PLEASE, if you see a problem with that logic, reply! :D
Mar 20 '06 #7

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

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
26
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: ...
5
by: Ali Baba | last post by:
Hi, Is there is equivalent of VB's DateDiff() method in C#. I need to find difference in months between two dates that are years apart. Docs says that I can use TimeSpan like: TimeSpam ts =...
5
by: Juan | last post by:
Hi everyone, is there a function that calculates the exact amount of Years, Months, and Days between two days? TimeDiff is not good enough, since it calculates, for example: Date 1: Dec....
3
by: MaRCeLO PeReiRA | last post by:
Hi Guys, I am in troubles with some dates. "I need to know the difference, in days, between two dates." Well, if the difference is less than a month, so I could use:
4
by: Working_Girl | last post by:
Hi, I have a database with insurance clients and their dependents (spouses and children). We had a problem in the past with the twins and some of them have been entered with one month...
23
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
2
by: =?Utf-8?B?Sm9lIFRhdmFyZXM=?= | last post by:
I am using the DateTime.Parse method to parse user entered dates. The dates can be just the year, Month-year and Month-day-year in a variety of formats in a variety of cultures. I immediately parse...
5
by: Mike | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...

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.