Time to GMT for any date

Hi!
It is easy to get the time offset to GMT from current date using time to GMT. But is there a way to get the time offset from GMT for any local date/time that is NOT the current (given the time zone set in System preferences)?
The problem I need to solve is that my the script I’m working on needs to know the time offset for varying date-times before and ahead of the point in time when the script is run, and that he time offset might vary depending on if daylight time saving is present or not at that point in time.
So I would like something that looks like this (where the results in the examples is the correct ones for “my” time zone, i.e. Stockholm, CET):

set theDateString to "2019-08-01 14:01:24"
set timeOffset to timeToGMT(date theDateString)
--> 7200

set theDateString to "2019-12-01 14:01:24"
set timeOffset to timeToGMT(date theDateString)
--> 3600

Try this:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions

set theDateString to "2019-08-01 14:01:24"
-- make date
set df to current application's NSDateFormatter's new()
df's setDateFormat:"yyyy-MM-dd HH:mm:ss" -- edited; see below
set theDate to df's dateFromString:theDateString
-- get offset
set tz to current application's NSTimeZone's localTimeZone() 
set theOffset to tz's secondsFromGMTForDate:theDate

Thanks. That works fine only if the hour of the input date is 12 or lower.
“2019-08-01 11:00:00” returns 7200
“2019-08-01 14:01:24”, as in the example, returns 3600
So it seems to be a problem with the 24 hour format.

Yes, sorry — that should be HH, not hh. I’ll correct it.

1 Like

Great! Thanks a lot!