Date-/Time-Calculations using AppleScript

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.

  1. set myNewDate to myDate + (2 * days), adds 2 days to the date and time stored in myDate.
  2. set myNewDate to myDate + (4 * hours), adds 4 hours to the current time and date.
  3. 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.

16 Comments

  1. Grover
    Posted September 28, 2008 at 5:43 pm | Permalink

    Bless you kind sir. I just spent hours looking for this information.

  2. Posted September 29, 2008 at 7:50 am | Permalink

    Glad to hear that. Apple has a tendency to “hide” crucial information, took a while to figure this out. 🙂

  3. Posted October 12, 2008 at 9:41 pm | Permalink

    Maybe you know how to get month as integer without the “if myMonth is january then set myM to 01”?

  4. Posted October 13, 2008 at 6:35 am | Permalink

    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.

  5. Drew
    Posted December 3, 2008 at 4:06 pm | Permalink

    What if I wanted to get a date in this format: “2008-12-02 16:03:45.180”?

  6. Posted January 14, 2009 at 1:14 am | Permalink

    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.

  7. Posted January 14, 2009 at 1:23 am | Permalink

    I even tried adding the -date switch to the defaults command, but it seems to use a different format

  8. Posted January 14, 2009 at 1:29 am | Permalink

    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

  9. Posted January 14, 2009 at 2:11 am | Permalink

    per:
    set mM to (month of (current date)) as integer
    will do it

  10. Scott
    Posted June 30, 2009 at 8:34 pm | Permalink

    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

  11. Posted July 2, 2009 at 7:33 am | Permalink

    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.

  12. Wesley Konrad
    Posted February 21, 2011 at 1:30 am | Permalink

    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.

  13. Posted February 21, 2011 at 8:48 am | Permalink

    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. 😉

  14. Dale Winstead
    Posted November 22, 2013 at 12:11 am | Permalink

    Thanks for this post.
    “set myTime to time of myDate
    set myNewDate to myDate – myTime”
    Exactly what I was looking for. So obvious, but it plum eluded me!

  15. Brian
    Posted March 3, 2014 at 7:27 pm | Permalink

    Sorry to resurrect such an old post, but I found it useful today.

    You can get the month as a number from a date by multiplying it by 1.

    set myDate to current date
    set myMonth to month of myDate
    set myMonth to myMonth * 1

    This will get the month number regardless of locale.

  16. Posted March 4, 2014 at 7:59 am | Permalink

    The idea of this blog is to keep the conversation alive, so thanks for your comment. 😉

    As usual with AppleScript, there is even another way:

    set myMonthNumber to (month of mydate as integer)

    Best,
    Erik

One Trackback

  1. […] 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 […]

Post a Comment

Your email is never published nor shared. Required fields are marked *, comments are moderated.

*
*