Getting the numeric month from the month name

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MichaelBluejay
Posts: 234
Joined: Thu Jul 01, 2010 11:50 am

Getting the numeric month from the month name

Post by MichaelBluejay » Wed Jan 06, 2021 3:09 am

Using this code, I can get the numeric month from the month name with 3 lines of code:

Code: Select all

put "Jan 1,Feb 2,Mar 3,Apr 4,May 5,Jun 6,Jul 7,Aug 8,Sep 9,Oct 10,Nov 11,Dec 12" into months
split months by comma and space
put months["Jan"]
But I have a hunch it's possible with only two lines of code. Is it?

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 119
Joined: Thu Apr 13, 2006 6:25 pm

Re: Getting the numeric month from the month name

Post by rkriesel » Wed Jan 06, 2021 3:29 am

Hi, Michael. How about one line?
get lineOffset(tMonth, the abbr monthNames)
-- Dick

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10318
Joined: Wed May 06, 2009 2:28 pm

Re: Getting the numeric month from the month name

Post by dunbarx » Wed Jan 06, 2021 4:55 am

Hah. I can do it in no lines.

It's "1".

Craig

MichaelBluejay
Posts: 234
Joined: Thu Jul 01, 2010 11:50 am

Re: Getting the numeric month from the month name

Post by MichaelBluejay » Wed Jan 06, 2021 6:50 am

Thanks, rkriesel, that does the trick.

I was trying to roll my own to convert dates like "Jul 12, 2020" to "7/12/20" because according to the dictionary, "Jul 12, 2020" is not one of the formats that LC can convert to or from, but trying it just now, in fact it can.

Code: Select all

convert "Jul 12, 2020" to short date
put it
--returns 7/12/20

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10318
Joined: Wed May 06, 2009 2:28 pm

Re: Getting the numeric month from the month name

Post by dunbarx » Wed Jan 06, 2021 4:51 pm

Hi.

There are lots of ways to present a date, and we have all had to swap parts around either to fit into one of the acceptable LC formats, or to take a format and fit into a custom one. The "dateItems" is usually your friend.

Craig

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Getting the numeric month from the month name

Post by Klaus » Wed Jan 06, 2021 5:06 pm

MichaelBluejay wrote:
Wed Jan 06, 2021 6:50 am
I was trying to roll my own to convert dates like "Jul 12, 2020" to "7/12/20" because according to the dictionary, "Jul 12, 2020" is not one of the formats that LC can convert to or from, but trying it just now...
-> Jul 12, 2020
IS in fact a date LC can deal with, it is named
-> the abbr date
(abbr = abbreviated for abbreviated :-))

Sometimes it is a good idea to tell LC what you want:

Code: Select all

convert "Jul 12, 2020" from abbr date to short date
We also have:
the long date
the internet date
the abbr monthnames
the abbr weekdaynames

And if you are not an english native speaker everything as above but with SYSTEM, gives me everything in german:
the long system date
the abbr system monthnames
the abbr system weekdaynames

MichaelBluejay
Posts: 234
Joined: Thu Jul 01, 2010 11:50 am

Re: Getting the numeric month from the month name

Post by MichaelBluejay » Fri Jan 08, 2021 8:53 am

Like I said, the format I want is not listed in the dictionary as one of the formats that LC can deal with.

The format I want ("Jan 8, 2021") is *not* "the abbr date". The "abbr date" returns a date like "Fri, Jan 8, 2021" (adding the "Fri" for the day of the week, which I don't want).

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Getting the numeric month from the month name

Post by Klaus » Fri Jan 08, 2021 10:55 am

Sorry, my fault!

Post Reply