Skip to main content
Solved

Last Saturday of the month

  • December 11, 2017
  • 9 replies
  • 118 views

dustin
Influencer
Forum|alt.badge.img+30

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.

Best answer by takashi

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.

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

9 replies

danilo_fme
Evangelist
Forum|alt.badge.img+44
  • Evangelist
  • December 11, 2017

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


takashi
Influencer
  • Best Answer
  • December 12, 2017

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.


Forum|alt.badge.img
  • December 12, 2017

Hi @cartoscro

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


dustin
Influencer
Forum|alt.badge.img+30
  • Author
  • Influencer
  • December 12, 2017
takashi wrote:

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.

 


dustin
Influencer
Forum|alt.badge.img+30
  • Author
  • Influencer
  • December 12, 2017
lenaatsafe wrote:

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!

 


dustin
Influencer
Forum|alt.badge.img+30
  • Author
  • Influencer
  • December 12, 2017

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?


takashi
Influencer
  • December 12, 2017
dustin wrote:

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?

 

 


dustin
Influencer
Forum|alt.badge.img+30
  • Author
  • Influencer
  • December 12, 2017
takashi wrote:
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

 

 

 

 


takashi
Influencer
  • December 15, 2017
dustin wrote:

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

 


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