Solved

DateDifferenceCalculator

  • 20 April 2017
  • 5 replies
  • 9 views

Hi,

I try to calculate the difference between to dates. I need the result in Years. From the FME HUb I get the

DateDifferenceCalculator. I runs good for difference in days or weeks. But how can I get the result in month or years?

icon

Best answer by ciarab 20 April 2017, 17:09

View original

5 replies

Badge

It looks like the units are set within the custom transformer as a parameter with an alias.

You can edit the definition of the parameter.

Within the configuration section, you can add a new choice with an alias. An alias would be the number of seconds in a month or year. I assume this wasn't included as the number of seconds in a month or year vary and therefore any output wouldn't be accurate.

 

Badge +3

see topic yesterday..

[clock format [clock scan today] -format {%j}] - [clock format [clock scan {YourAttribute}] -format {%j}]

(difference compared to today..)

replace %j (for days of year) with %W (for weeks of year) to get weekdifference.

To calculate daydifference over years ( over yearboundaries) you need to check if year is a leapyear

Or do clock scan and then use one of common calculations.

Badge +9

The DateTimeCalculator in 2017 allows you to do this quite easily

Userlevel 4

If you can't upgrade to FME 2017 just yet, here's a quick Python solution for a PythonCaller. Expects the input attributes "start_date" and "end_date" on the format YYYYMMDD, outputs the attribute "difference_in_years":

from dateutil.relativedelta import relativedelta
from datetime import datetime

def CalculateYearsElapsed(feature):
    start_date_str = feature.getAttribute("start_date")
    end_date_str = feature.getAttribute("end_date")
    start_date = datetime.strptime(start_date_str, '%Y%m%d')
    end_date = datetime.strptime(end_date_str, '%Y%m%d')
    difference_in_years = relativedelta(end_date, start_date).years
    feature.setAttribute("difference_in_years", difference_in_years)

Thank you for your help. It runs.

Reply