[Qgis-user] Calculate age in years with expressionbuilder

Stefan Giese (WhereGroup) stefan.giese at wheregroup.com
Mon Dec 18 23:57:38 PST 2017


I forgot: and as a workaround you can use:
year(todate(now()) - year(todate("GDate"))
which will produce correct ages, or you can use your own python function 
(expression builder->functions->new file):

from qgis.core import *
from qgis.gui import *

from datetime import datetime
from dateutil.relativedelta import relativedelta

@qgsfunction(args=2, group='Python')
def GetAge(values,feature,parent):
     '''
     call this function GetAge("fromdate","todate")
     in years
     '''
     d1 =datetime.strptime(values[0],"%Y-%m-%d")
     d2 =datetime.strptime(values[1],"%Y-%m-%d")
     difference = relativedelta(d2, d1)
     return difference


Am 2017-12-19 07:48, schrieb Stefan Giese (WhereGroup):
> it seems, that this is a bug, see: https://issues.qgis.org/issues/17681
> 
> best regards
> Stefan
> 
> 
> Am 2017-12-18 15:26, schrieb Reginald:
>> hello,
>> 
>> I am calculating the age of persons with the expression builder.
>> My formulae looks like this:
>> year(age( to_date(now()) , "GDate" ))
>> The result looks alright for people who are younger than 68 years.
>> People who are born in 1949 or before get negative ages and the age 
>> goes
>> from -68 to -31 (for someone born in 1913)
>> What is wrong with my formulae?
>> 
>> Regards,
>> 
>> 
>> 
>> 
>> 
>> 
>> --
>> Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-User-f4125267.html
>> _______________________________________________
>> Qgis-user mailing list
>> Qgis-user at lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user



More information about the Qgis-user mailing list