Hi,
Im trying to calculate age. I got the year part down, but I also need to know the months..
Im born 17 January, 1982, so the result I want is 27 years and 3 months (since we havent passed 17 April yet, then it would be 4 months)
But how do I get the months value ?
I appreciate any help I can get ![]()
-
-
It looks like you're trying to get the lexical difference between two dates (the lexical distance between 1 Jan and 1 Feb is the same as the distance between 1 Feb and 1 Mar - 1 month, even though it's a different number of days)
You've actually made a mistake though - it's 3 months only after we get to April 17: (Jan17 --> Feb17 = 1 month, Feb17 --> Mar17 = 2month, Mar17 --> Apr17 = 3month)To do it, you just do what you do for (long) subtraction (writing the date in yyyy/mm/dd format is best)2009 / 04 / 06- 1982 / 01 / 17------------------Y / M / DD = 06 - 17, but that's negative, so we need to 'borrow' one from the month category. The number of days 'borrowed' is the number of days in the previous month:2009 / 03 / 37- 1982 / 01 / 17---------------Y / M / 20For M, we don't need to 'borrow' months from the previous column, since 3 > 1, but if we did, we would borrow the number of months in the previous year, which is always going to be 12.The result is then2009 / 03 / 37- 1982 / 01 / 17-----------------27 / 02 / 20So you are 27 years, 2 months and 20 days old -
Thanks!evildictaitor said:It looks like you're trying to get the lexical difference between two dates (the lexical distance between 1 Jan and 1 Feb is the same as the distance between 1 Feb and 1 Mar - 1 month, even though it's a different number of days)You've actually made a mistake though - it's 3 months only after we get to April 17: (Jan17 --> Feb17 = 1 month, Feb17 --> Mar17 = 2month, Mar17 --> Apr17 = 3month)To do it, you just do what you do for (long) subtraction (writing the date in yyyy/mm/dd format is best)2009 / 04 / 06- 1982 / 01 / 17------------------Y / M / DD = 06 - 17, but that's negative, so we need to 'borrow' one from the month category. The number of days 'borrowed' is the number of days in the previous month:2009 / 03 / 37- 1982 / 01 / 17---------------Y / M / 20For M, we don't need to 'borrow' months from the previous column, since 3 > 1, but if we did, we would borrow the number of months in the previous year, which is always going to be 12.The result is then2009 / 03 / 37- 1982 / 01 / 17-----------------27 / 02 / 20So you are 27 years, 2 months and 20 days old
-
Console.WriteLine(DateTime.Now - DateTime.Parse("1982-01-17"));
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.