Understanding Hard-Coded Date Behavior in AppleScript

applescripttimezone

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.

Best Answer

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