Skip to main content
Question

Finding name of Month between two dates

  • February 11, 2020
  • 1 reply
  • 41 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?

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

1 reply

david_r
Celebrity
  • 8392 replies
  • February 11, 2020

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