This short how-to explains how you calculate with time- and date-values in AppleScript. Quite a few people told me, that they are using the do shell script-handler presented in the post How to convert an “epoch”-time to a meaningful date and time. Nothing wrong with that, but if you just want to perform time-calculations inside an AppleScript, then there is an easier way.
If you assign the current time to a variable as in set myDate to current date and you just want to calculate the current date plus 2 days, then there is no need to use epoch-seconds.
set myNewDate to myDate + (2 * days), adds 2 days to the date and time stored inmyDate.set myNewDate to myDate + (4 * hours), adds 4 hours to the current time and date.set myNewDate to myDate + (30 * minutes), adds 30 minutes to the current time and date.
It is really that simple, AppleScript uses epoch-seconds to perform these calculations, so you don’t have to.
If you need to set the date and time to the beginning of the current day, then use
set myTime to time of myDate
set myNewDate to myDate - myTime
The variable myNewDate now contains the current date and 00:00:00 as time.
Epoch-seconds are great if you want to store a date-value and read it back in later, but as long as you stay inside an AppleScript, there is no need to resort to epoch-values.
13 Comments
Bless you kind sir. I just spent hours looking for this information.
Glad to hear that. Apple has a tendency to “hide” crucial information, took a while to figure this out.
Maybe you know how to get month as integer without the “if myMonth is january then set myM to 01″?
Per, the fastest way I’m aware of is:
set theDate to current date
copy theDate to b
set the month of b to January
set monthNum to (1 + (theDate – b + 1314864) div 2629728)
“monthNum” should now contain the month as int.
Hope this helps.
What if I wanted to get a date in this format: “2008-12-02 16:03:45.180″?
I’m trying to write a simple app for my start up folder that checks to see whether this is the first time I’ve booted up today so it can open stuff that I need when I first get in to work, but which I don’t want to see if I restart during the day.
I do it by getting the app to store the last login date in my preferences, and compare the last login with today. But If I do
do shell script (“defaults write org.pureandapplied.startupscript ‘lastopen’ ‘” & (current date) & “‘”)
set lastopen to do shell script (“defaults read org.pureandapplied.startupscript ‘lastopen’”)
day of lastopen
it doesn’t treat the value returned as a date:
“Can’t make \”Wednesday, 14 January 2009 12:05:29 PM\” into type date.”
I can do it with text munging, or by checking the modification date on a file, but that’s nowhere near as elegant.
I even tried adding the -date switch to the defaults command, but it seems to use a different format
Oh I got it. I had the shell scripts in a subroutine and was doing
on readdate()
set lastopen to do shell script (”defaults read org.pureandapplied.startupscript ‘lastopen’”)
return lastopen
end
But for some reason the subroutine doesn’t get evaluated as a date. If I force it to return a date it works, so I use
return date lastopen
not
return lastopen
per:
set mM to (month of (current date)) as integer
will do it
so I’m trying to write and applescript to compare the create and modified time stamps of a file
so I’m talking about a format like
2009-03-09 18:03:47 -0400
and
2009-03-09 15:03:47 -0700
which are for all intents and purposes are equal, but apple script wont let me cast them as date type. are there any built-ins or tricks for evaluating this kind of format with applescript or even the date command from a terminal?
thanks
Scott, let me think about this. There are a couple of solutions popping into my head right now. I’ll write a post about the subject.
I came up with a faster solution to return the month as an integer
set bob to current date
set intMonth to (word 2 of (short date string of bob)) as integer
Although this will only work depending on how the text item delimiters are set.
You seem to be living in a DDMMYY locale, meaning your solution will work for people living in such a locale. In the US the short date string looks like this MMDDYY, so it wouldn’t work as expected or worse, result in a wrong value going unnoticed.
One should always be on the lookout for such problems and circumvent them by being as locale-agnostic as possible.
One Trackback
[...] trying to subtract numbers from the date in the script I eventually found help at Erik’s Lab: Date-/Time-Calculations using AppleScript. That gave me the very simple 2 lines I needed to subtract 20 hours from my time to get Santa [...]