how to get difference between 2 dates in years:months format?
-
Hi all, AM working on a C# project.. my requirement is as follows: there are 2 dates viz date1 & date2. if date1 > date2 the difference should come as e.g (3years,2months) if date1 < date2 the difference should come as e.g (-3years,-2months) I am able to get the first scenario but the second scenario I am having problems with, not able to calculate the number of months in negative quite correctly... Below is the code used in case of positive scenario,currentdate is alwys greater than the assetCapDate passed as a value: public string newmethod(string assetCapDate) { string timeStr = string.Empty; int years = 0; int months = 0; int days = 0; TimeSpan ts = new TimeSpan(); System.DateTime date1 = DateTime.Now; System.DateTime date2 = Convert.ToDateTime(assetCapDate); ts = date1.Subtract(date2); years = (ts.Days/365); do { for(int i=0; i <= 12; i++) { if(date1.Subtract(date2.AddYears(years).AddMonths(i)).Days >=0) { months = i; } else { break; } } if(months > 12) years = years + 1; }while(months > 12); days = date1.Subtract(date2.AddYears(years).AddMonths(months)).Days; if (years > 0) { timeStr += years.ToString() + " Years, "; } if (months > 0) { timeStr += months.ToString() + " Months"; } if (days > 0) { timeStr += days.ToString() + " Days"; } return(timeStr); } Any help would be highly appreciated. I am not sure whether this question falls under C# domain..Moderators please advise accordingly..will move this question to another domain if required.
Regards Anurag
-
Hi all, AM working on a C# project.. my requirement is as follows: there are 2 dates viz date1 & date2. if date1 > date2 the difference should come as e.g (3years,2months) if date1 < date2 the difference should come as e.g (-3years,-2months) I am able to get the first scenario but the second scenario I am having problems with, not able to calculate the number of months in negative quite correctly... Below is the code used in case of positive scenario,currentdate is alwys greater than the assetCapDate passed as a value: public string newmethod(string assetCapDate) { string timeStr = string.Empty; int years = 0; int months = 0; int days = 0; TimeSpan ts = new TimeSpan(); System.DateTime date1 = DateTime.Now; System.DateTime date2 = Convert.ToDateTime(assetCapDate); ts = date1.Subtract(date2); years = (ts.Days/365); do { for(int i=0; i <= 12; i++) { if(date1.Subtract(date2.AddYears(years).AddMonths(i)).Days >=0) { months = i; } else { break; } } if(months > 12) years = years + 1; }while(months > 12); days = date1.Subtract(date2.AddYears(years).AddMonths(months)).Days; if (years > 0) { timeStr += years.ToString() + " Years, "; } if (months > 0) { timeStr += months.ToString() + " Months"; } if (days > 0) { timeStr += days.ToString() + " Days"; } return(timeStr); } Any help would be highly appreciated. I am not sure whether this question falls under C# domain..Moderators please advise accordingly..will move this question to another domain if required.
Regards Anurag
-
Use the Math.Abs()[^] method to get the absolute value of the difference when converting to years and months.
One of these days I'm going to think of a really clever signature.
hi Richard..thxx for the suggestion..i did try Math.Abs stuff but still not getting the desired result... The conversion into months in negative scenario is what I am not getting.. Further assistance would be highly appreciated..
Regards Anurag
-
Hi all, AM working on a C# project.. my requirement is as follows: there are 2 dates viz date1 & date2. if date1 > date2 the difference should come as e.g (3years,2months) if date1 < date2 the difference should come as e.g (-3years,-2months) I am able to get the first scenario but the second scenario I am having problems with, not able to calculate the number of months in negative quite correctly... Below is the code used in case of positive scenario,currentdate is alwys greater than the assetCapDate passed as a value: public string newmethod(string assetCapDate) { string timeStr = string.Empty; int years = 0; int months = 0; int days = 0; TimeSpan ts = new TimeSpan(); System.DateTime date1 = DateTime.Now; System.DateTime date2 = Convert.ToDateTime(assetCapDate); ts = date1.Subtract(date2); years = (ts.Days/365); do { for(int i=0; i <= 12; i++) { if(date1.Subtract(date2.AddYears(years).AddMonths(i)).Days >=0) { months = i; } else { break; } } if(months > 12) years = years + 1; }while(months > 12); days = date1.Subtract(date2.AddYears(years).AddMonths(months)).Days; if (years > 0) { timeStr += years.ToString() + " Years, "; } if (months > 0) { timeStr += months.ToString() + " Months"; } if (days > 0) { timeStr += days.ToString() + " Days"; } return(timeStr); } Any help would be highly appreciated. I am not sure whether this question falls under C# domain..Moderators please advise accordingly..will move this question to another domain if required.
Regards Anurag
The general way to get the difference between two DateTime values is just to subtract and get a TimeSpan. A TimeSpan only goes as high as Days, not months or years because doing so is rather difficult -- you must take the varying lengths of months and Leap Days and into account. It were easy and reliable, it would be built-in. If you really want to do this, you're on your own. I seem to recall that someone else asked about this a few months back, but a quick search didn't turn it up.
-
The general way to get the difference between two DateTime values is just to subtract and get a TimeSpan. A TimeSpan only goes as high as Days, not months or years because doing so is rather difficult -- you must take the varying lengths of months and Leap Days and into account. It were easy and reliable, it would be built-in. If you really want to do this, you're on your own. I seem to recall that someone else asked about this a few months back, but a quick search didn't turn it up.
PIEBALDconsult wrote:
you must take the varying lengths of months and Leap Days and into account.
I'm not saying that it's right, or that anyone should do it, but what about the Datediff function in the Microsoft.VisualBasic namespace? There you'r able to get the difference in months and years without that hassle. But saying that, I've not used that namespace in a C# solution.
-
hi Richard..thxx for the suggestion..i did try Math.Abs stuff but still not getting the desired result... The conversion into months in negative scenario is what I am not getting.. Further assistance would be highly appreciated..
Regards Anurag
anurag3487 wrote:
The conversion into months in negative scenario is what I am not getting.
I'm not sure what you mean by this but using a
TimeSpan
object will get you the number of days difference, and using theAbs()
method will get you the number as a positive integer. Converting that into months and years requires some logic which takes into account leap years and the number of days in each month, none of which is impossible, it just requires a little extra thought.One of these days I'm going to think of a really clever signature.
-
Hi all, AM working on a C# project.. my requirement is as follows: there are 2 dates viz date1 & date2. if date1 > date2 the difference should come as e.g (3years,2months) if date1 < date2 the difference should come as e.g (-3years,-2months) I am able to get the first scenario but the second scenario I am having problems with, not able to calculate the number of months in negative quite correctly... Below is the code used in case of positive scenario,currentdate is alwys greater than the assetCapDate passed as a value: public string newmethod(string assetCapDate) { string timeStr = string.Empty; int years = 0; int months = 0; int days = 0; TimeSpan ts = new TimeSpan(); System.DateTime date1 = DateTime.Now; System.DateTime date2 = Convert.ToDateTime(assetCapDate); ts = date1.Subtract(date2); years = (ts.Days/365); do { for(int i=0; i <= 12; i++) { if(date1.Subtract(date2.AddYears(years).AddMonths(i)).Days >=0) { months = i; } else { break; } } if(months > 12) years = years + 1; }while(months > 12); days = date1.Subtract(date2.AddYears(years).AddMonths(months)).Days; if (years > 0) { timeStr += years.ToString() + " Years, "; } if (months > 0) { timeStr += months.ToString() + " Months"; } if (days > 0) { timeStr += days.ToString() + " Days"; } return(timeStr); } Any help would be highly appreciated. I am not sure whether this question falls under C# domain..Moderators please advise accordingly..will move this question to another domain if required.
Regards Anurag
-
thanks a lot everybody for the responses.. will do evrything what is required to crack it...
-
Hi all, AM working on a C# project.. my requirement is as follows: there are 2 dates viz date1 & date2. if date1 > date2 the difference should come as e.g (3years,2months) if date1 < date2 the difference should come as e.g (-3years,-2months) I am able to get the first scenario but the second scenario I am having problems with, not able to calculate the number of months in negative quite correctly... Below is the code used in case of positive scenario,currentdate is alwys greater than the assetCapDate passed as a value: public string newmethod(string assetCapDate) { string timeStr = string.Empty; int years = 0; int months = 0; int days = 0; TimeSpan ts = new TimeSpan(); System.DateTime date1 = DateTime.Now; System.DateTime date2 = Convert.ToDateTime(assetCapDate); ts = date1.Subtract(date2); years = (ts.Days/365); do { for(int i=0; i <= 12; i++) { if(date1.Subtract(date2.AddYears(years).AddMonths(i)).Days >=0) { months = i; } else { break; } } if(months > 12) years = years + 1; }while(months > 12); days = date1.Subtract(date2.AddYears(years).AddMonths(months)).Days; if (years > 0) { timeStr += years.ToString() + " Years, "; } if (months > 0) { timeStr += months.ToString() + " Months"; } if (days > 0) { timeStr += days.ToString() + " Days"; } return(timeStr); } Any help would be highly appreciated. I am not sure whether this question falls under C# domain..Moderators please advise accordingly..will move this question to another domain if required.
Regards Anurag
I think code "ts.Days/365" is not right,leap year should be considered
-
I think code "ts.Days/365" is not right,leap year should be considered
hi all..have found the solution to the problem above posted by me..thxx evryone fr their suggestions..will post the answer sometime...