Skip to main content
Solved

DateDifferenceCalculator


jtn40764
Participant
Forum|alt.badge.img

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?

Best answer by ciarab

The DateTimeCalculator in 2017 allows you to do this quite easily

View original
Did this help you find an answer to your question?

5 replies

Forum|alt.badge.img

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.

 


gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • April 20, 2017

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.


ciarab
Contributor
Forum|alt.badge.img+9
  • Contributor
  • Best Answer
  • April 20, 2017

The DateTimeCalculator in 2017 allows you to do this quite easily


david_r
Celebrity
  • April 20, 2017

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)


jtn40764
Participant
Forum|alt.badge.img
  • Author
  • Participant
  • April 21, 2017

Thank you for your help. It runs.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings