如何计算出生日期的年龄 [英] How to calculate age from date of birth

查看:166
本文介绍了如何计算出生日期的年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MySql中计算从出生日期开始的年,月和日的年龄。



i使用现有查询来获得结果,例如

How to calculate age in Year,Month and Days from date of birth in MySql.

i used an existing query to get the result,like

select TIMESTAMPDIFF( YEAR, dojDate,now()) as years
    , TIMESTAMPDIFF( MONTH, dojDate,now()) % 12 as months
    , FLOOR( TIMESTAMPDIFF( DAY, dojDate,now()) % 30.4375 ) as days





但问题是,

假设



but the issue is that,
Suppose

DOJ : '2010-01-01'
NOW : '2011-01-02'
O/P : 1 Year ,0 Month, 0 Days
Correct. 

DOJ : '2010-01-01'
NOW : '2011-01-01'
O/P : 1 Year ,0 Month, 30 Days
Actually this should return 0 Year, 11 Month, 30 Days



如何获得这个?


How to get this??

推荐答案

计算使用

选择DATEDIFF(yy,'1980-12-31',getdate())
calculate using
select DATEDIFF(yy, '1980-12-31', getdate())


使用此查询:



use this query :

select extract(year from from_days(days)) - 1600 as years
     , extract(month from from_days(days)) - 1 as months
     , extract(day from from_days(days)) - 1 as days
  from ( select to_days(now()) - to_days(a.dojDate) +
                to_days(str_to_date('1600-01-01', '%Y-%m-%d')) as days
           from sampletable a ) as b







http://sqlfiddle.com/#!2/8d1a6/12 [参考]


请参考此[已解决]代码项目链接

如何使用C#计算年龄 [ ^ ]



A来自StackTrace的其他人

http:// stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c [ ^ ]
Please refer this [solved] code project link
How to calculate Age using C#[^]

Another one from StackTrace
http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c[^]


这篇关于如何计算出生日期的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆