429,251 Members | 2,764 Online
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

# I have 2 dates - How old is the user (days, months and years)

 P: 55 I have two dates. I need to determine if the user is over 18 based on the date they entered. I assume I have to do something like subtract their birthdate from today, but its not exactly that straight forward. Any suggestions or a code sample you could point me to? That would be excellent! Thanks :) Sep 24 '09 #1
4 Replies

 Expert 100+ P: 1,501 If your dates are in DateTime objects, you can use the TimeSpan object. Here's an quick snippet that calculates how many years old I am... Expand|Select|Wrap|Line Numbers             DateTime bd = new DateTime(1982, 1, 9);             DateTime now = DateTime.Now;               TimeSpan ts = now - bd;             Console.WriteLine("Years: " + ts.Days / 365.0); I hope that helps! Sep 24 '09 #2

 Expert 5K+ P: 7,872 Note to Gary, you will want to use the .TotalDays property, not the .Days. Sep 24 '09 #3

 P: 12 Why can't you subtract the user entered date from System.DateTime.Now? I mean ... first get the Year component of both the dates and then subtract. Then you can check whether the difference is greater than 18 or not. Tell me if it works. Sep 24 '09 #4

 Expert 100+ P: 1,501 @Plater Oops, yes... Apparently I left that there from when I was testing, trying to figure out why it was telling me I was 28 years old. Let this be a lesson to all, there are 365 days in a year and 360 degrees in an angle, not the other way around. ;) Expand|Select|Wrap|Line Numbers DateTime bd = new DateTime(1982, 1, 9); DateTime now = DateTime.Now;   TimeSpan ts = now - bd; Console.WriteLine("Years: " + ts.TotalDays / 365.0); @akshaymahajan You could probably do it... you should be able to try it out yourself fairly easily :) Sep 24 '09 #5