Skip to main content

Is there a function or method for returning the date of the last Saturday of any given month? Input attribute would be formatted 'April 2018' or 'September 2019'.

I'm currently doing this by manually inputting the correct dates in an AttributeCreator conditional statement. I'm looking for a method that would not require conitinual maintenance.

Hi @cartoscro,

What do you wnat is similar like this ?

https://stackoverflow.com/questions/12796389/python-get-last-monday-of-july-2010

This was implemented using python.

Thanks,

Danilo


Hi @cartoscro, assuming that the month is given with the format "<month name in English><space><four digits year>" (e.g. "December 2017"), this procedure does the trick (FME 2017.1+).

  1. Add "<space>01" to the month string to create a date value which represents the first day of the given month. e.g. "December 2017 01".
  2. Convert the format (%B %Y %d, in the example above) of the date to the FME standard date format (%Y%m%d).
  3. Add 1 month to the date to get the first day of the next month.
  4. Convert the format of the first day of the next month to the format of Sunday-based weekday number (%w), which is in the range from 0 (Sunday) to 6 (Saturday).
  5. Subtract (weekday number + 1) days from the first day of the next month. The resulting date is the last Saturday of the given month.

If you feel the workflow above is too cluttered, it's time to learn the lovely FME Date/Time Functions.

The Date/Time Expressions

_first_day_of_next_month@DateTimeAdd(@DateTimeParse(@Value(month) 01,%B %Y %d),P1M)_weekday@DateTimeFormat(@Value(_first_day_of_next_month),%w)_last_saturday_of_mongh@DateTimeAdd(@Value(_first_day_of_next_month),-P@Evaluate(@Value(_weekday)+1)D)

Hope this helps.


Hi @cartoscro

we now have PreviousSaturdayCalculator custom transformer available on FME Hub. Using @takashi's approach paired with this transformer, the workflow will be:


Hi @cartoscro, assuming that the month is given with the format "<month name in English><space><four digits year>" (e.g. "December 2017"), this procedure does the trick (FME 2017.1+).

  1. Add "<space>01" to the month string to create a date value which represents the first day of the given month. e.g. "December 2017 01".
  2. Convert the format (%B %Y %d, in the example above) of the date to the FME standard date format (%Y%m%d).
  3. Add 1 month to the date to get the first day of the next month.
  4. Convert the format of the first day of the next month to the format of Sunday-based weekday number (%w), which is in the range from 0 (Sunday) to 6 (Saturday).
  5. Subtract (weekday number + 1) days from the first day of the next month. The resulting date is the last Saturday of the given month.

If you feel the workflow above is too cluttered, it's time to learn the lovely FME Date/Time Functions.

The Date/Time Expressions

_first_day_of_next_month@DateTimeAdd(@DateTimeParse(@Value(month) 01,%B %Y %d),P1M)_weekday@DateTimeFormat(@Value(_first_day_of_next_month),%w)_last_saturday_of_mongh@DateTimeAdd(@Value(_first_day_of_next_month),-P@Evaluate(@Value(_weekday)+1)D)

Hope this helps.

@takashi Thank you sir. It works brilliantly.

 


Hi @cartoscro

we now have PreviousSaturdayCalculator custom transformer available on FME Hub. Using @takashi's approach paired with this transformer, the workflow will be:

@LenaAtSafe Thanks!

 


I think I may have discovered a bug in putting this together.

For the month June and July, our output will be the full name. For all other months, the 3-letter English name will be output. So I assumed that for '_last_saturday_of_the_month', I could set a conditional value to format separately the two cases. However, I can't call on the 'weekday' or 'first day of the next month' attributes. Is this expected, or should I be able to access these from the conditional window?


I think I may have discovered a bug in putting this together.

For the month June and July, our output will be the full name. For all other months, the 3-letter English name will be output. So I assumed that for '_last_saturday_of_the_month', I could set a conditional value to format separately the two cases. However, I can't call on the 'weekday' or 'first day of the next month' attributes. Is this expected, or should I be able to access these from the conditional window?

Newly created attributes with an AttributeCreator/Manager seem not to appear in the conditional value setting dialog of the same transformer. If you need to refer those attributes in a conditional value setting, add another AttributeCreator/Manager to define that.

 

By the way, for June and July, the @DateTimeFormat function with the format specifier %b returns their 3-letter month name i.e. "Jun" and "Jul" as expected, in my environment - FME 2017.1.1.1. Which version of FME are you using?

 

 


Newly created attributes with an AttributeCreator/Manager seem not to appear in the conditional value setting dialog of the same transformer. If you need to refer those attributes in a conditional value setting, add another AttributeCreator/Manager to define that.

 

By the way, for June and July, the @DateTimeFormat function with the format specifier %b returns their 3-letter month name i.e. "Jun" and "Jul" as expected, in my environment - FME 2017.1.1.1. Which version of FME are you using?

 

 

Thanks for the reply. @DateTimeFormat function is working as it should for me. However, for those two months, my output needs to be spelled out completely. So my output months need to read:

 

 

Jan

 

Feb

 

Mar

 

Apr

 

May

 

June

 

July

 

Aug

 

Sep

 

Oct

 

Nov

 

Dec

 

 

 

 


I think I may have discovered a bug in putting this together.

For the month June and July, our output will be the full name. For all other months, the 3-letter English name will be output. So I assumed that for '_last_saturday_of_the_month', I could set a conditional value to format separately the two cases. However, I can't call on the 'weekday' or 'first day of the next month' attributes. Is this expected, or should I be able to access these from the conditional window?

I got it. I don't think there is any convenient Data/Time function to format only "June" and "July" in full name and format others in 3-letter name. However, you can change "Jun" and "Jul" within a date string to "June" and "July" easily with the StringPairReplacer after formatting all month names with 3-letter.

 

  • Replacing Pairs: Jun June Jul July

 


Reply