current date - current month text value to numerical value

Hello all,

what I have:

  1. dialog input that makes the user click last, current or next month which assigns the text output as March, April or May

what I would like

  1. take the output month and convert March, April, May (or any month) to either 2, 3 or 4 (corresponding number of month) without creating a list that has all the months listed.

any help would be greatly appreciated.

This will work:

(current date)'s month as number

The same syntax will also work on a date property:

(date "21 May 1961")'s month as number

Good luck,

thank craig for the reply. I apologize for not being clearer. What you provided me is fine but not exactly what I was requesting.

Essentially this is the flow.

dialog displays 3 buttons

“March” “April” and “May”

they clicked one of the corresponding buttons the return value is

“March” “April” or “May”

say you picked “March”

Is there a way to convert March without a list to 3? or May to 5? as the newly assigned value.

I hope that clarifies.

Your suggestion although helpful only works to convert the current month’s to a number not say the month ahead or the month before.

maybe too much info.

Just trying to figure out a way to convert the Months (January, February, March, etc) to numerical value of 1, 2, 3 or etc.

Hi,

this way depends on the format settings in International PrefPane, this works in english

set b to button returned of (display dialog "Pick a month" buttons {"April", "May", "June"})
month of date ("1 " & b & " 2007") as integer --> 4, 5 or 6

Again, assuming English:

set m to button returned of (display dialog "Choose a month:" buttons {"April", "May", "June"})
(offset of m's text 1 thru 3 in "  JanFebMarAprMayJunJulAugSepOctNovDec") div 3

Or:

set m to button returned of (display dialog "Choose a month:" buttons {"April", "May", "June"})
(run script m) as integer

This is pretty cool, Kai, and it even works independent from international date format settings,
because the run script command results one of AppleScript’s built-in month constants

thanks again :slight_smile:

Kai’s other method is about 110 times as fast, though. :slight_smile:

I had a look earlier at using the same offset technique with various language/format combinations.

A fair amount of date string parsing was needed to negotiate certain formats (such as Gaeilge/Éire and Português/Portugal/Brasil), but FWIW:


property default_month : missing value

on local_month_data()
	tell «data isot3131313130313031» as date
		set l to words of (it as Unicode text)
		tell words of ((it + 23587200) as Unicode text)
			set b to 1
			repeat while (item b is l's item b)
				set b to b + 1
			end repeat
			set e to -1
			repeat while (item e is l's item e)
				set e to e - 1
			end repeat
		end tell
		set l to {}
		set s to "  "
		repeat with n from 1 to 12
			tell text from word b to word e of ((it + n * 2419200) as Unicode text)
				set l's end to it
				set s to s & text 1 thru 3
			end tell
		end repeat
		{l, s}
	end tell
end local_month_data

considering case
	set {month_list, month_index} to local_month_data()
	if default_month is missing value then set default_month to month_list's beginning
	repeat
		set chosen_month to choose from list month_list with prompt "Choose a month:" default items default_month
		if chosen_month is false then error number -128
		set default_month to chosen_month's beginning
		set month_number to (offset of (default_month's text 1 thru 3) in month_index) div 3
		display dialog "You chose month " & month_number & ": " & default_month & "."
	end repeat
end considering

Edit: Code adjusted to incorporate some suggestions by Nigel Garvey here.