I have a script that runs on the first day of every month and looks at data from since the beginning of the current year. When January rolls around each year I need the script to look at the previous years data. Is there a better way to write this?
set currentDate to date "Friday, January 1, 2021 at 12:00:00 AM"
set m to month of currentDate as text
if m is "January" then
set firstOfYear to date "Saturday, January 1, 2000 at 12:00:00 AM"
set year of firstOfYear to (year of currentDate) - 1
else
set firstOfYear to date "Saturday, January 1, 2000 at 12:00:00 AM"
set year of firstOfYear to year of currentDate
end if
Your script works only on a system using the English date.
Here is a version working with every format.
set currentDate to my buildDate(2021)
set m to month of currentDate as integer
set firstOfYear to my buildDate(2020)
if m = 1 then
set year of firstOfYear to (year of currentDate) - 1
else
set year of firstOfYear to year of currentDate
end if
firstOfYear
on buildDate(theYear)
set adate to "1/1/1"
set adate to date adate
set year of adate to theYear
return adate
end buildDate
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 5 juin 2020 21:42:38
Thank you very much. In looking at other posts I tried using the “1/1/1” approach but it returns a different date than expected (Screen Shot). Is there a setting somewhere that I am missing?
set currentDate to (current date)
copy currentDate to firstOfYear
tell firstOfYear to set {its day, its month, its time} to {1, January, 0}
if (month of currentDate is January) then set year of firstOfYear to (year of firstOfYear) - 1
return firstOfYear
As your screenshot is unavailable I can’t see what you got so I am unable to guess what was wrong.
Here is my script with some log instructions showing what I get.
case currentdate in january
set currentDate to my buildDate(2021)
log currentDate (*date vendredi 1 janvier 2021 à 00:00:00*)
set m to month of currentDate as integer
set firstOfYear to my buildDate(2020)
log firstOfYear (*date mercredi 1 janvier 2020 à 00:00:00*)
if m = 1 then
set year of firstOfYear to (year of currentDate) - 1
else
set year of firstOfYear to year of currentDate
end if
firstOfYear --> date "mercredi 1 janvier 2020 à 00:00:00"
on buildDate(theYear)
set adate to "1/1/1"
set adate to date adate
set year of adate to theYear
return adate
end buildDate
case currentdate in june
set currentDate to my buildDate(2021)
set month of currentDate to 6 -- ADDED to check if the behavior is right
log currentDate (*date mardi 1 juin 2021 à 00:00:00*)
set m to month of currentDate as integer
log m (*6*)
set firstOfYear to my buildDate(2020)
log firstOfYear (*date mercredi 1 janvier 2020 à 00:00:00*)
if m = 1 then
set year of firstOfYear to (year of currentDate) - 1
else
set year of firstOfYear to year of currentDate
end if
firstOfYear --> date "vendredi 1 janvier 2021 à 00:00:00"
on buildDate(theYear)
set adate to "1/1/1"
set adate to date adate
set year of adate to theYear
return adate
end buildDate
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 6 juin 2020 04:26:25
If you remember, there’s a bug which shows up when converting between AppleScript date objects and date strings when the years are very early. There can quite a difference between the date objects’ properties and what the strings say. The amount and direction of the difference varies according to which part of the world you’re in. Here in the UK, I get results like this on my Mojave system:
set adate to "1/1/1"
set adate to date adate --> date "Wednesday 1 January 1 at 00:00:00"
-- But:
tell adate to get {its year, its month, its day, its time}
--> {0, December, 30, 75} -- ie. 30th December 1 BC at 00:01:15.
set adate's year to 2020
return adate
--> date "Wednesday 30 December 2020 at 00:01:15"
I gather from your previous reports that the bug doesn’t manifest in the Central European time zone, but in other parts of the world, it can seriously affect the chances of your handler returning the intended date.