AppleScript is unable to get a date as string
Solution 1:
This may not be a direct solution or quite the answer you are looking for, but this following AppleScript code provide examples of how to convert a given short date string into an actual date, how to extract the elements of the date, edit and set date and time values, and to create your own custom date and time as a string. Hope this helps.
set dateAsText to "9/23/16"
-- converts dateAsText into an actual date
set someDate to date dateAsText -- date "Friday, September 23, 2016 at 12:00 AM"
-- extract the short date from someDate... as a string
set shortDate to short date string of someDate -- "09/23/16"
-- change the time value of someDate from "12:00 AM" to "5:00 PM"
set adjustedDate to date "5:00 PM" of someDate -- date "Friday, September 23, 2016 at 5:00 PM"
-- extract elements from adjustedDate... as strings
set theTime to time string of adjustedDate -- "5:00 PM"
set theWeekday to weekday of adjustedDate -- Friday
set theWeekday to (weekday of adjustedDate) as string -- "Friday"
set theDay to day of adjustedDate -- 23
set theDay to (day of adjustedDate) as string -- "23"
set theMonth to month of adjustedDate -- September
set theMonth to (month of adjustedDate) as string -- "September"
-- edit values of adjustedDate
set day of adjustedDate to 14 -- date "Wednesday, September 14, 2016 at 5:00 PM"
set year of adjustedDate to 1978 -- date "Thursday, September 14, 1978 at 5:00 PM"
------------
set customDate to short date string of adjustedDate & space & theTime -- "09/14/78 5:00 PM"
Solution 2:
It seems string value
(not value
) is the term you're looking for. Excel stores cells in its raw format (e.g., date, number, etc.) but then has the separate ability to format those values. So, for example, you could store the number 2.33423
but display it as 2.3
showing just one decimal.
In Applescript, this is the difference between the term value
(which is the raw value the way Excel stored it) and string value
which is the way Excel displays it.
tell application "Microsoft Excel"
get value of active cell
-- date "Sunday, May 19, 2019 at 12:00:00 AM"
get string value of active cell
-- "5/19/19"
end tell
Now, provided you have already set the target formatting on the cell, you can set the cell contents simply, by something like this:
tell application "Microsoft Excel"
set value of active cell to "6/18/2019"
end tell
Provided this cell is formatted as a date, it will interpreted as such.
Getting back to the original issue at hand, you can review AppleScript's class definitions for the class date
-- scroll down until you find the entire description. The reason that month
appears differently, is that it is a constant not an integer. Date is a class type inside AppleScript, which has associated properties you can get or manipulate.
For example: One could get the month as an integer in the following way (otherwise, it will be a constant).
tell application "Microsoft Excel"
set theDate to value of active cell as date
get (month of theDate) as integer
end tell
Additional references:
- Excellent discussion on AppleScript time and date formatting.
- Additional sample code on setting the date on an Excel cell. I'm not sure this is needed, but it shows you how you can do it.