Current date previous month without list

Objective:

  1. Trying to get the previous month name WITHOUT defining a list.

Question:

  1. Can you get “February” instead of the integer or do you have to define the list value with all the months with some additional parameter?

tell application "Finder"
	set monthValue to ((current date)'s month) - 1
end tell
-- Actual Results in "2"

-- Desired result is "February"

Edit: Nevermind, I get it. Use kai’s answer.

Perhaps something like this, slashdot:

tell (current date) to set lastMonth to month of (it - day * days) as string
--> "February"

thanks kai. Exactly what I wanted.

Does anyone know how to get the next month?

I know this works but was looking for another dynamic version.


tell (current date) to set nextMonth to month of (it + 30 * days) as string

thanks

Hi slashdot,

Your last script won’t work for certain months (e.g. July).

gl,

Thanks. That’s why I submitted it since I knew that it may not work for like leap years or varing months. just don’t know how to get the current number of days in the month through applescript.

Hi slashdot,

Kai’s script gets the last day of last month relative to the current date. To get next month you need to use the index of the current month and add 1. For instance, this month has index 6. I’m using Jaguar, but in newer versions of os, there are easier ways to get the index. Here’s how I might do it in Jaguar:


set m_list to ¬
	{January, February, March, April, May, June, July, August, September, October, November, December}
set m to month of (current date)
repeat with mi from 1 to 12
	set list_m to item mi of m_list
	if list_m is m then exit repeat
end repeat
-- 12 + 1 = 13
set next_m to (mi mod 12) + 1
set as_m to item next_m of m_list

gl,

I really should make it more modular.


set i_list to {}
repeat with i from 0 to 24
	set end of i_list to GetNextMonth(i)
end repeat
return i_list
--
on GetNextMonth(n)
	set m_list to ¬
		{January, February, March, April, May, June, July, August, September, October, November, December}
	set m to month of (current date)
	repeat with mi from 1 to 12
		set list_m to item mi of m_list
		if list_m is m then exit repeat
	end repeat
	set next_m to (mi + n) mod 12
	if next_m is 0 then set next_m to 12
	set as_m to item next_m of m_list
	return as_m
end GetNextMonth

If you want a modular subroutine, then you might make it so you can use negative numbers for previous months.

gl,

This’ll do it:

tell (current date) to set nextMonth to (month of (it + (32 - (its day)) * days)) as string

Hi Nigel,

I was wondering when you would step in. How would you change it to say 12 months from now?

Thanks,

I came up with this to use negative and positive indices, but I’m thinking there might be a better way.


set n to -12 -- 12 months ago
set n to (n + ((((n div 12) ^ 2) ^ 0.5) + 1) * 12)

gl,

Er, wouldn’t that be the same month that it is now?

Is there any reason you can’t increase 32 to a larger value?

I see you just made another post while I was looking at this; I’ll have to try that out too.

Hi, kel. On the font page of this site (ie. MacScripter.net) at the moment, there’s an article by Adam in which he’s been kind enough to quote several scripts from my DateTips package in ScriptBuilders. There’s one there that does calendar month arithmetic and which might serve the turn. (Get the month of the date it returns.)

Hi Nigel,

Thanks anyway, but I finally got a formula that works to get n months ago or after:


set l to {}
repeat with n from -26 to 26
	set n to (n + ((((n div 12) ^ 2) ^ 0.5) + 1) * 12) mod 12
	if n is 0 then set n to 12
	set end of l to n
end repeat
return l

gl,

Hi,

Sorry, but gotta rush. Here’s the subroutine:


set l to {}
repeat with i from -25 to 25
	set end of l to GetMonthFromCurrent(i) -- i relative to current month index
end repeat
return l
--
on GetMonthFromCurrent(n)
	set m_list to {January, February, March, April, May, June, July, August, September, October, November, December}
	set cur_date to (current date)
	set cur_m to month of cur_date
	repeat with m from 1 to 12
		set list_m to item m of m_list
		if list_m is cur_m then exit repeat
	end repeat
	set n to ((n + ((((n div 12) ^ 2) ^ 0.5) + 1) * 12) + m) mod 12
	if n is 0 then set n to 12
	return (item n of m_list) as string
end GetMonthFromCurrent

Nigel, I’ll check out your subroutines later. Need to go somewhere.

Thanks,

Any thoughts?

GetMonthFromCurrent(1)

on GetMonthFromCurrent(n)
	if n < 0 then set n to n + 1
	tell (current date) to return (month of (it + (32 * n - (its day)) * days)) as string
end GetMonthFromCurrent

GetMonthFromDate(-1, date "Sunday, 29 February 2004 12:00:0 AM")

on GetMonthFromDate(n, _date)
	if n < 0 then set n to n + 1
	tell _date to return (month of (it + (32 * n - (its day)) * days)) as string
end GetMonthFromDate

Hi, Bruce. That works very well up to about 18 or 19 months either side of the reference date. Beyond that, too many 32s are added into the mix and it begins to overshoot the target month. Since the handler’s only after the month rather than the precise date, it can mod n by 12 when it receives it. It also needs to special-case n parameter values of 0, so that it returns the current month rather than the previous one. (I don’t think anyone’s specifically going to call it with a parameter of 0, but if it’s called in a repeat as in Kel’s script, it’ll need to cope.)

on GetMonthFromCurrent(n)
	set n to n mod 12
	if n is 0 then return (month of (current date)) as string
	if n < 0 then set n to n + 1
	tell (current date) to return (month of (it + (32 * n - (its day)) * days)) as string
end GetMonthFromCurrent

Thanks Nigel, I knew that had to happen at some point. By the way, I like your information from the front page. :cool:

I had to write that article in a rush as I had had pneumonia during the time I would normally have consulted with Nigel about it. I’ve found his Date&Time scripts to be endlessly useful, both because they do very clever things and because I’ve needed to make some of those calculations to write scripts for iCal to do things that iCal doesn’t do natively.

Adam