Current date previous month without list

Not really, Bruce - I suppose the value could be any integer between 32 and 59. It has to be greater than 31 so that, when the date’s day (potentially 1 to 31) is subtracted from it, the resulting value is greater than 0. The maximum value should be no greater than the total number of days in any 2 month period (the lowest value of which will be 28 + 31 = 59) - otherwise there’s a risk of spilling over into the month that follows next month.

Call me perverse, Mr G. - but I’m afraid I did. :wink:

From AppleScript 1.10 (Mac OS X version 10.4) you could also utilise AppleScript’s ability to express a date’s month property as an integer:
Tiger only:

to GetMonthFromCurrent(n)
	tell (current date)
		set its month to ((its month as integer) + 11 + n mod 12) mod 12 + 1
		its month as string
	end tell
end GetMonthFromCurrent

However, since that seems a bit slower here than your suggestion, I’m tempted to resort to this (rather lengthy but slightly faster) one-liner:
Tiger only:

to GetMonthFromCurrent(n)
	item ((((current date)'s month as integer) + 11 + n mod 12) mod 12 + 1) of {"January", "February", ¬
		"March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
end GetMonthFromCurrent

My apologies, Mr E. I meant “anyone in their right mind”. :wink:

Ah yes. Only one property extracted from the date, month-to-integer instead of the slower month-to-string coercion, integer maths instead of date maths, and a list of “pre-coerced” strings. Pretty deadly. :slight_smile:

If I recall correctly, month-to-integer coercions were introduced with Panther, so this script should work there too. It’s the ability to set a date’s month to a number (as in your first script) that’s new with Tiger.

But even those with comparatively ancient systems can enjoy the fun: :wink:

to GetMonthFromCurrent(n)
	tell (current date)
		copy it to b
		set b's month to January
		item (((b - 2500000 - it) div -2500000 + 11 + n mod 12) mod 12 + 1) of {"January", "February", ¬
			"March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
	end tell
end GetMonthFromCurrent

(I note though, that a solution involving a list is a little off-topic for this thread!) :rolleyes:

Hi Everybody,

I finally read through all the posts and found it an interesting evolution. Nigel, I’ve read through your article before and it has a lot of useful info. One thing I don’t like about the vanilla month index is that it’s hard to understand. That’s why I always use the repeat loop and month list and as I remember, the time test were not that much more faster. I’ll try some testing again.

slashdot want to find months relative to todays date without the use of lists. However if using lists is more efficient then it seems to me that it still relates to this post. After all, the lilst only contains 12 items. I didn’t know that coercion from applescript month to month string mattered that much.

Thanks a lot for all input,

gl,

That’s OK, then - lets me off the hook, anyway. :lol:

Quite. Be a pity if anyone missed out on those breathtaking “scriptobatics” of yours. :smiley:

Technically, yes - you’re right. The topic was started so long ago - and this is a bit of a sub-thread, anyway. If we keep this just between ourselves, perhaps nobody will notice… :stuck_out_tongue:

Hi Kai,

I don’t think you need to worry because administators and moderators don’t read these long threads. :slight_smile: Wait I must be seeing things. Nearly everyone has a title. :o ← surprised

Looks like we’re surrounded, kel. :wink: