Jump to content

Calculating Month Number Based on Month Name


This topic is 6538 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hello,

As I am trying to expand my experiance, I'm trying to make calculations harder on myself so I can learn. Here is what I did with finding the month number from a Month Name:




Let (

list =  ValueListItems ( Get ( FileName ) ; "Month List" );



ValueCount ( Left (list ; Position ( list ; Month::SelectMonth ; 1 ; 1 ) + Length ( Month::SelectMonth ) ) )



)

SelectMonth Values:

January

Febraury

March

April

May

June

July

August

September

October

November

December

I didn't want to hardcode any months even though I could have used a simple Case function; what other ways are there to calculate a month number based on its name?

Thanks and God Bless!

David

Link to comment
Share on other sites

I can think of several, but when you say you don't want to hardcode, it begs the question of language. You yourself have hardcoded the ENGLISH month names into a value list. What if the user is French?

Another question is the input method: in English, the first 3 letters of the month name are enough to identify the month. So if you limit yourself to English, you could use the same formula for short or full month name as the input, for example:

Let ( [

string = "janfebmaraprmayjunjulaugsepoctnovdec" ;

mmm = Left ( Input ; 3 )

] ;

Ceiling ( Position ( string ; mmm ; 1 ; 1 ) / 3 )

)

It wouldn't be too hard to compile a list of 12 month names in the current language, but I don't know the minimum length required to identify the month in each languages.

Link to comment
Share on other sites

Hi

I know the you own FM8 and not the Advanced version, but isn't to difficult to ask someone to make a black file with this custom inside...

This custom function makes what you want and is *NOT* depending from the used language.

------------------------

/*

MonthNameToMonth custom function

Author: Daniele Raybaudi

Format: MonthNameToMonth ( theMonthName ; start )

Parameters:

theMonthName - text

start - number (must be set to 1)

Returns the month number of the given monthName

*/

Let([

date = Date ( start ; 1 ; 1 );

month = Month ( date )

];

Case(

start > 12 or IsEmpty ( theMonthName ) ; "?" ;

MonthName ( date ) = theMonthName ; month ;

MonthNameToMonth ( theMonthName ; start + 1 )

)

)

Link to comment
Share on other sites

This topic is 6538 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.