[Qgis-user] Calculate age in years with expressionbuilder

Reginald Carlier Reginald.Carlier at ingelmunster.be
Tue Dec 19 00:14:01 PST 2017


Thanks Stefan,

I finally got the age by using this code:

(2017 - year(to_date("GDate")))

Year(to_date(now()) seems to fail as there are missing brackets but I can't figure out where.

Regards,


Reginald Carlier
Deskundige GIS
Gemeente Ingelmunster, Oostrozebekestraat 4, 8770 Ingelmunster
T+32 51 33 74 39www.ingelmunster.be
Raadpleeg onze disclaimer 
-----Oorspronkelijk bericht-----
Van: Stefan Giese (WhereGroup) [mailto:stefan.giese at wheregroup.com] 
Verzonden: dinsdag 19 december 2017 8:58
Aan: Reginald Carlier
CC: qgis-user at lists.osgeo.org
Onderwerp: Re: [Qgis-user] Calculate age in years with expressionbuilder

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20171219/5098eaf3/attachment.html>


More information about the Qgis-user mailing list