Age in years with decimal precision given a datetime

前端 未结 6 1921
-上瘾入骨i
-上瘾入骨i 2021-01-06 03:37

How can I get the age of someone given the date of birth in a C# datetime.

I want a precise age like 40.69 years old

6条回答
  •  鱼传尺愫
    2021-01-06 03:49

    This would be an approximative calculation:

     TimeSpan span = DateTime.Today.Subtract(birthDate);
     Console.WriteLine( "Age: " + (span.TotalDays / 365.25).toString() );
    

    BTW: see also this question on Stack Overflow: How do I calculate someone’s age in C#?

提交回复
热议问题