SQLite:如何计算出生日期的年龄

问题描述 投票:10回答:7

在sqlite3中从出生日期开始计算年龄的最佳方法是什么?

sqlite date
7个回答
13
投票
SELECT (strftime('%Y', 'now') - strftime('%Y', Birth_Date)) - (strftime('%m-%d', 'now') < strftime('%m-%d', Birth_Date));

这个有效。 我只是移植了这个MySQL示例:http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html


11
投票

您可以将日期转换为浮点数并减去...

cast(strftime('%Y.%m%d', 'now') - strftime('%Y.%m%d', dob) as int)

...其中doba valid SQLite time string


4
投票

只是为了澄清kai's answer(似乎编辑现在非常不受欢迎,并且评论具有强大的大小限制和格式问题):

SELECT (strftime('%Y', 'now') - strftime('%Y', Birth_Date)) 
     - (strftime('%m-%d', 'now') < strftime('%m-%d', Birth_Date) );

让我们假设我们想要整整几年:在生命的第一个日历年,这将是零。

对于第二个日历年,这将取决于日期 - 即它将是:

  • 1为strftime('%m-%d', 'now') >= strftime('%m-%d', Birth_Date) [“案例A”]
  • 否则为0 [“案例B”];

(我假设sqlite对两个日期字符串进行了字典比较,并返回一个整数值。)

对于任何其他日历年,它将是当前和第二年之间的年数 - 即strftime('%Y', 'now') - strftime('%Y', Birth_Date) - 1,加上与上述相同。

换句话说,只有当“案例A”的相反情况发生时,我们才会从strftime('%Y', 'now') - strftime('%Y', Birth_Date)中减去1 - 也就是说,当且仅当strftime('%m-%d', 'now') < strftime('%m-%d', Birth_Date),因此公式。


1
投票

我发现这可能会对你有所帮助:

select
    case
        when date(dob, '+' ||
            strftime('%Y', 'now') - strftime('%Y', dob) ||
            ' years') >= date('now')
        then strftime('%Y', 'now') - strftime('%Y', dob)
        else strftime('%Y', 'now') - strftime('%Y', dob) - 1
    end
    as age
from t;

来自http://www.mail-archive.com/[email protected]/msg20525.html


0
投票

要获得@Bruno Costa的工作答案,我必须在第4行的strftime差异计算周围添加括号,并将比较反转为'now'小于或等于:据我所知,计算是否计算出计算日期在今天之前或之后。如果计算的日期是今天或更早,那么生日已经是今年或今天:

select
    case
        when date(dob, '+' ||
            (strftime('%Y', 'now') - strftime('%Y', dob)) ||
            ' years') <= date('now')
        then strftime('%Y', 'now') - strftime('%Y', dob)
        else strftime('%Y', 'now') - strftime('%Y', dob) - 1
    end
    as age
from t;

请注意,与@Bruno Costa的答案相关联的解决方案未经测试。


0
投票

另一种解决方案是使用年份%j作为一年中的一小部分,将其除以365.0。所以你最终得到:

select strftime('%Y', 'now') + strftime('%j', 'now') / 365.2422) - (strftime('%Y', Birth_Date) + strftime('%j', Birth_Date) / 365.2422);

然后,您可以将结果转换为整数:

select CAST(strftime('%Y', 'now') + strftime('%j', 'now') / 365.2422) - (strftime('%Y', Birth_Date) + strftime('%j', Birth_Date) / 365.2422) AS INT);

或使用ROUND()来舍入结果:

select round(strftime('%Y', 'now') + strftime('%j', 'now') / 365.2422) - (strftime('%Y', Birth_Date) + strftime('%j', Birth_Date) / 365.2422));

我更新了计算,因此它使用太阳年中正确的十进制天数,如Robert在下面的评论中所述。

与其他计算的不同之处在于,此计算的结果是十进制数,可以转换为整数(以获得确切的年数)或舍入(以获得大致的年数)。

对于生日来说,大概的年数可能不相关,但是对于你购买的东西的年龄更有意义。


-1
投票

在sqlite中很难做到...所以你最好从数据库nd中检索出生日期,然后添加这个逻辑

private Calendar mConvert_Date_To_Calendar(String dateToConvert)
{
    // TODO Auto-generated method stub
    try
    {
        Calendar calConversion = Calendar.getInstance();
        Date date1 = null;
        try
        {
            date1 = new SimpleDateFormat("dd/MM/yy").parse(dateToConvert);
        }
        catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        calConversion.setTime(date1);
        System.out.println("mConvert_Date_To_Calender date1: " + date1
                + "caleder converted done:" + calConversion.getTime());
        return calConversion;
    }
    catch (Exception e)
    {
        e.printStackTrace();
        Log.i(TAG, "" + e);
        return null;
    }
}


private Calendar myCurrentCalender()
{
    try
    {
        Calendar myCurrentCal = Calendar.getInstance();
        myCurrentCal.set(myCurrentCal.get(Calendar.YEAR), myCurrentCal.get(Calendar.MONTH),
                myCurrentCal.get(Calendar.DAY_OF_MONTH));
        return myCurrentCal;
    }
    catch (Exception e)
    {
        e.printStackTrace();
        Log.e(TAG, "Unable to get current calendar!");
        return null;
    }
}

public int daysBetween(Date d1, Date d2)
{
    System.out.println("calculated age :"
            + (((d2.getTime() - d1.getTime()) / ((24 * 1000 * 60 * 60)) / 365)));

    return (int) (((d2.getTime() - d1.getTime()) / (24 * 1000 * 60 * 60)) / 365);
}

第一个函数将以日历格式计算出生日期,第二个函数将计算当前日期,第三个函数将查找两者之间的差异。如下所示,调用这些函数

dateOfBirth = mConvert_Date_To_Calendar(age);

currentDate = myCurrentCalender();

candidateAge = daysBetween(dateOfBirth.getTime(),currentDate.getTime());

“candidateAge”将具有年龄。

© www.soinside.com 2019 - 2024. All rights reserved.