Question

Finding name of Month between two dates

  • 11 February 2020
  • 1 reply
  • 7 views

Hi there,

 

Is there any way to calculate which month to come between two dates. I am using the date time calculator to calculate the number of months between two dates. However, I am interested in finding which Months lies between two dates. For instance, between 20-2-2020 and 1-4-2020, March (03) comes. Is there any way of doing that in FME?


1 reply

Userlevel 4

You can e.g. use the following in a PythonCaller:

import fmeobjectsimport datetimedef monthrange(start, finish):  months = (finish.year - start.year) * 12 + finish.month + 1   for i in range(start.month+1, months-1):    year  = (i - 1) // 12 + start.year     month = (i - 1) % 12 + 1    yield datetime.date(year, month, 1).strftime('%m')    def FeatureProcessor(feature):    start = str(feature.getAttribute('start_date') or '')    end = str(feature.getAttribute('end_date') or '')    start_dt = datetime.datetime.strptime(start, '%Y%m%d')    end_dt = datetime.datetime.strptime(end, '%Y%m%d')    feature.setAttribute('months{}', list(monthrange(start_dt, end_dt)))

Assuming the input attribute start_date and end_date on the FME format %Y%m%d, it will return a list months{} containing all the month numbers between the two dates, the months of the start and end date not inclusive. Be careful about what happens when you roll over to a new year.

Here's also a workspace template for FME 2019.2.2: monthrange.fmwt

Reply