In C#, how do I calculate someone's age based on a DateTime type birthday?

后端 未结 30 2094
名媛妹妹
名媛妹妹 2020-11-21 05:14

Given a DateTime representing a person\'s birthday, how do I calculate their age in years?

30条回答
  •  清酒与你
    2020-11-21 05:41

    SQL version:

    declare @dd smalldatetime = '1980-04-01'
    declare @age int = YEAR(GETDATE())-YEAR(@dd)
    if (@dd> DATEADD(YYYY, -@age, GETDATE())) set @age = @age -1
    
    print @age  
    

提交回复
热议问题