3

I am designing an AppleScript which contains a hard-coded date to which the script refers multiple times throughout the duration of the execution. An example is as follows:

property someDate : date "Monday, 1 January 2018 at 01:00:00 PM"

As one can see, the format of the timestamp is appropriate for my region and country. Another example would be: Saturday, April 15, 2006 4:29:04 PM or 12/25/04 - which is the format for other regions such as the US - mm/dd/yy.

The following questions arise

  1. If my AppleScript will be run in other regions/countries (those with different timestamp formats eg. mm/dd/yy instead of my dd/mm/yy), how does AppleScript behave with the above declaration?
  2. How should I ensure that AppleScript will correctly convert the timestamp from my format into the respective format used by the end user's device?

The proper conversion becomes critical if the format of the date changes to the previously mentioned dd/mm/yy and is then used in a mm/dd/yy environment (region or country). In that case, the order may not be apparent.

Example Case

property someDate : date "02/01/18" as dd/mm/yy would be hard-coded with the intention of representing 02 January 2018, but when used in a different region (those using mm/dd/yy); naturally, this will become 01 February 2018. See the issue?

As my research into this leads nowhere fast (and should be specific to AppleScript), I hope somebody who has implemented similar code could assist me in clarifying the two points above. Thank you.

5
  • Okay, setting my system to New Zealand, where I believe you live, and compiling property someDate : date "Monday, 1 January 2018 at 01:00:00 PM" in Script Editor the ` at` gets dropped, but compiles. Also current date returns the same format but without the at. That said, setting it back to my normal setting it does not compile and returns Syntax Error Invalid date and time date Monday, 1 January 2018 at 01:00:00 PM.. I which you'd answer the question I previously asked, which was: Why are you setting a property to a fixed date, what purpose does it serve in the script? Jan 1, 2018 at 21:06
  • My intention is to code a base-date from which others can be calculated (e.g. days until this expiry date). Using a property seems appropriate as it should not change once compiled; however, I guess set someDate to date "..." would work too (thus not limiting myself to a property declaration). I hope that answers the last part. Another way could be to set someDate to the current date set the month of someDate to 1 set the day of someDate to 2 set the year of someDate to 2018. Hoping to not only write a concise timestamp but also learn the proper way of implementing a global solution.
    – ProGrammer
    Jan 2, 2018 at 0:58
  • I had a feeling that's what you were doing. One of the ways is to record the current date in epoch time to a plist file and use that value to calculate against. I suppose it could be stored in a property upon first run. Either way you'd be able to calculate against it. Jan 2, 2018 at 1:23
  • I see your thinking but that's no good as a validation if it can be adjusted by editing the plist file. I think I'll give the alternative option (mentioned in my previous comment) a try, perhaps that way all can be contained within the one document and work throughout the regions. I can't think of any other "safe" ways to accomplish the proper conversion.
    – ProGrammer
    Jan 2, 2018 at 5:15
  • I never said it was good, only one of the ways developers do it! You can set it as property but do it in epoch time and not human readable date like you're trying to do. Jan 2, 2018 at 14:27

1 Answer 1

2

This works for me using the latest version of Sierra

--property the_date : current date
property the_date : date ("Sunday, May 31, 2009 at 4:00:00PM")
property the_year : year of the_date
property the_day : day of the_date
property the_month : month of the_date
property the_hours : hours of the_date
property the_minutes : minutes of the_date
property the_seconds : seconds of the_date
property shortDateString : missing value

make_date(the_year, the_month, the_day, the_hours, the_minutes, the_seconds)

set shortDateString to short date string of the_date
set timeString to time string of the_date
set systemDate to date string of date shortDateString
log timeString
log shortDateString
log systemDate

on make_date(the_year, the_month, the_day, the_hours, the_minutes, the_seconds)
    --set the_date to current date
    set year of the_date to the_year
    set day of the_date to 1
    set month of the_date to the_month
    set day of the_date to the_day
    set hours of the_date to the_hours
    set minutes of the_date to the_minutes
    set seconds of the_date to the_seconds
    return the_date
end make_date

In this script, I set a predetermined date value as a variable. With my region set up to United States, in system preferences, I ran the script and logged the results. The first three lines of the results in the following image, were the values returned with my system set to United States region. Next I went back to system preferences and switched my region to Hungary and ran the script again. The last three lines of the results in the image, reflect the date and time values as if I was on a computer in Europe (Hungary)

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .