Applescript returning wrong month from Numbers

My Numbers table contains dates that are the the first of the month and I need to use the month name in a script. If Cell B2 contains a date, entered as "1 May 2019" without the quotes, the following AppleScript returns "April" rather than "May".


activate application "Numbers"
tell application "Numbers"
	tell table 1 of sheet 1 of front document
		tell row 2
			set x to value of cell 2
			set xx to month of x
		end tell
	end tell
end tell


Logging x shows that the actual date/time value in B2 is an hour before midnight


get value of cell 2 of row 2 of table 1 of sheet 1 of document 1
		--> date "Tuesday, 30 April 2019 at 11:00:00 PM"


So I'm wondering why this is, and what is the best way to avoid the problem. Putting

set x to value of cell 2 as date

doesn't help.



MacBook Pro Retina

Posted on Oct 9, 2019 4:00 AM

Reply
5 replies

Oct 9, 2019 5:56 AM in response to jbh2

It's hard to say exactly without knowing settings on your Mac but very likely an adjustment is being made for daylight savings time or time to GMT.


You can adjust x in AppleScript by an hour via


set x to x - 60 * 60


... being the number of seconds in an hour.


In AppleScript you can get the offset to GMT, again expressed in seconds, with:


 time to GMT



SG



Oct 9, 2019 12:54 PM in response to SGIII

That's it! Changing my location to a city lacking daylight savings in the same time zone seems to prove it.


The DAYNAME function in Numbers works ok so it seems to be an AppleScript problem.


This looks like a nasty little bug - I can see no good reason for this behaviour and will send feedback to Apple. At the very least it warrants a mention in the AppleScript reference documents (forgive me if it's there but i haven't found it ...)


It will be necessary to check whether daylight saving is in effect each time the script is run.


Many thanks for your help.


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript returning wrong month from Numbers

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.