MSDN example [modified] (oops, my bad!)
-
Why not just use something like this?
public static int GetAge(string birthDate)
{
DateTime birth;
TimeSpan livingAge; /* This will be inaccurate by the time it's
* been calculated but I don't think the user
* will care about a few ticks */if(!DateTime.Parse(birthDate, out birth)) throw new InvalidCastException("Unable to parse birth date"); livingAge = DateTime.Now - birth; return (livingAge + DateTime.MinValue).Years - 1;
}
-
Cool, not what I had in mind, but definitely better than what I had in mind! :)
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 2 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
Computafreak wrote:
For what its worth, what did you have in mind?
For some reason I thought Timespan had a TotalYears property... :doh:
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 2 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
Cool, not what I had in mind, but definitely better than what I had in mind! :)
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 2 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))yet incorrect, since it assumes months and leap years evolve from day of birth in the same way as they do from DateTime.MinValue The net result is that close to one's birthday the formula can be off by 1. This one is much safer, although still not perfect:
DateTime.Today.Subtract(DateTime.Parse(birthday)).Days/365.242374
The perfect solution really uses ToDay.Years-DateTime.Parse(birthday).Years and adjusts by one according to the result of ToDay.DayOfYear < DateTime.Parse(birthday).DayOfYear :) -
Why not just use something like this?
public static int GetAge(string birthDate)
{
DateTime birth;
TimeSpan livingAge; /* This will be inaccurate by the time it's
* been calculated but I don't think the user
* will care about a few ticks */if(!DateTime.Parse(birthDate, out birth)) throw new InvalidCastException("Unable to parse birth date"); livingAge = DateTime.Now - birth; return (livingAge + DateTime.MinValue).Years - 1;
}
-
yet incorrect, since it assumes months and leap years evolve from day of birth in the same way as they do from DateTime.MinValue The net result is that close to one's birthday the formula can be off by 1. This one is much safer, although still not perfect:
DateTime.Today.Subtract(DateTime.Parse(birthday)).Days/365.242374
The perfect solution really uses ToDay.Years-DateTime.Parse(birthday).Years and adjusts by one according to the result of ToDay.DayOfYear < DateTime.Parse(birthday).DayOfYear :) -
Actually, after carefully looking at your solution, it appears to be wrong.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 2 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
Yes. Ok. This is a bit easier on the eye...
public static int GetAge(string birthDate) { DateTime birth; try { birth = DateTime.Parse(birthDate); } catch (Exception ex) { throw new ApplicationException("Unable to read birth date.", ex); } DateTime now = DateTime.Today; if (now.DayOfYear < birth.DayOfYear) { // Before your birthday return now.Year - birth.Year - 1; } return now.Year - birth.Year; }
Oh, and it works around midnight, too...
-
public static int GetAge(string birthDate)
{
DateTime birth;try
{
birth = DateTime.Parse(birthDate);
}
catch (Exception ex)
{
throw new ApplicationException("Unable to read birth date.", ex);
}if (DateTime.Today.Month < birth.Month) //it is before your birthday
{
return DateTime.Today.Year - birth.Year - 1;
}
else if (DateTime.Today.Month > birth.Month) //it is after your birthday
{
return DateTime.Today.Year - birth.Year;
}
else // DateTime.Today.Month == birth.Month //we don't know yet
{
if (DateTime.Today.Day < birth.Day) //it is before your birthday
{
return DateTime.Today.Year - birth.Year - 1;
}
else //it is your birthday, or it is after your birthday
{
return DateTime.Today.Year - birth.Year;
}
}
}:sigh: :doh: To be seen @ http://msdn.microsoft.com/en-us/library/bb126477.aspx[^] UPDATE: I was under the impression, Timespan had a TotalYears property. But now, looking at the logic, I can see why it does not have one. :-O
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 2 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))modified on Wednesday, April 15, 2009 10:40 AM
public static int GetAge(string birthDate)
{
throw new CoderIsABoorException();
}Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Yes. Ok. This is a bit easier on the eye...
public static int GetAge(string birthDate) { DateTime birth; try { birth = DateTime.Parse(birthDate); } catch (Exception ex) { throw new ApplicationException("Unable to read birth date.", ex); } DateTime now = DateTime.Today; if (now.DayOfYear < birth.DayOfYear) { // Before your birthday return now.Year - birth.Year - 1; } return now.Year - birth.Year; }
Oh, and it works around midnight, too...
For that approach to work, wouldn't you have to add 306 days to both the birthdate and the present time before using the Year and DayOfYear properties? The notion that someone born on 2/28/2000 would have had to wait 366 days to be "one year old", while someone born on 3/1/2000 would only have to wait 365 days, is somewhat artificial but it is certainly well-established. #2/28/2000#.DayOfYear == 59 #2/28/2001#.DayOfYear == 59 #3/1/2000#.DayOfYear == 61 #3/1/2001#.DayOfYear == 60 #2/28/2000#.AddDays(306).DayOfYear == 365 #2/28/2001#.AddDays(306).DayOfYear == 365 #3/1/2000#.AddDays(306).DayOfYear == 1 #3/1/2001#.AddDays(306).DayOfYear == 1 Of course, adding 306 days has an ugliness all its own. My own preference would probably be to simply format the thing as MMDDHHMMSS, do a string compare, and adjust the year appropriately, but another option would be to format as YYYYMMDDHHMMSS, convert to a long or double, subtract, and divide by 10,000,000,000.