• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Coping with Date format variations
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Coping with Date format variations


  • Subject: Re: Coping with Date format variations
  • From: "Mark J. Reed" <email@hidden>
  • Date: Sun, 23 May 2010 18:33:05 -0400

On Sun, May 23, 2010 at 6:12 PM, Gil Dawson <email@hidden> wrote:
Is there a way to code a literal date into a script in such a way that the same text would compile on both machines?

Nope.  If you compile it on either machine, it will run fine on the other, but if you want to share source code  text, you can't use the date literal string syntax.  

My usual solution is to do something like this:

set myDate to current date
tell myDate to set  {its month, its day, its year} to {October, 31, 2015}

But as written, that's not a completely general solution.  Order matters, because it sets each component individually and error-corrects the others. Consider this example:

set mydate to current date
set mydate to mydate + 7 * days
log (mydate)
(*date Sunday, May 30, 2010 6:30:16 PM*)
tell mydate to set {its month, its day, its year} to {February, 15, 2011}
mydate
-->  date "Tuesday, March 15, 2011 6:30:16 PM"

As soon as it set "month" to February, the date would have been illegal (Feb 30), so it shifted to March 2nd.  Then it set the day and year, yielding the above incorrect final result.   This isn't a problem if you're setting a date in October or any other month with 31 days, but the general solution is to take an extra step to set the month to one of those even when it's not the ultimate goal:

set myDate to current date
tell myDate  
    set its month to January -- or any other month with 31 days
    set {its day, its year} to {targetDay, targetYear}
    set its month to targetMonth
end tell


--
Mark J. Reed <email@hidden>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Coping with Date format variations (From: Gil Dawson <email@hidden>)

  • Prev by Date: Coping with Date format variations
  • Next by Date: Re: Coping with Date format variations
  • Previous by thread: Coping with Date format variations
  • Next by thread: Re: Coping with Date format variations
  • Index(es):
    • Date
    • Thread