Shane, thanks for this great tip.
I have updated my standard date handler accordingly.
Hey guys, please try to shoot holes in this script.
I didn't use stringToList because I don't care about the delimiters, only the position of the actual date/time data.
Plus, this allows more flexibility in decoding the date/time string.
I'm sure the code could be more compact, but I'm more interested in readability than compactness.
use AppleScript
version
"2.4" -- Yosemite (10.10) or later
use scripting additions
--- ENTER THE DATE IN THE FORMAT THAT IS SET IN YOUR SYSTEM PREFERENCES ---
set dateInYourLocale
to convertToDate("02/01/2017")
--- INTERNATIONAL DATE FORMAT ---
set dateISO
to convertToDate("2017-01-02")
log ("dateInYourLocale: " &
dateInYourLocale
as
text)
log ("dateISO: " &
dateISO
as text)
--- NOW TRY INVALID DATE FORMAT FOR U.S. ---
-- (but should work for Australia)
set dateAus
to convertToDate("31/01/2017")
--> error
### RESULTS ###
(*dateInYourLocale: Wed, Feb 1, 2017 at 12:00:00 AM*)
(*dateISO: Mon, Jan 2, 2017 at 12:00:00 AM*)
(*
[ERROR]
INVALID Date Format: 31/01/2017
*)
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- HANDLERS (functions)
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on convertToDate(pDate)
-- @Date @Convert @String
(* VER: 1.1 2017-03-02
---------------------------------------------------------------------------------
PURPOSE: Convert Date String to AppleScript Date Type
PARAMETERS:
• pDate | text OR date | Date to check/convert
RETURNS: date │ AppleScript date
AUTHOR: JMichaelTX
—————————————————————————————————————————————————————————————————————————————————
*)
-- MUST NOT BE IN ANY APP WHEN USING THE date FUNCTION --
if (class
of pDate =
date) then
set
dateAsDate to
pDate
else
-- Convert date string
try
-- Try Standard date format as set in System Preferences --
set
dateAsDate to
date (pDate)
on
error
--- Try International Date format (YYYY-MM-DD)
set dateAsDate
to my convertISOtoDate(pDate)
end try
end if
return
dateAsDate
end convertToDate
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on convertISOtoDate(pDateStr)
-- @Date @ISO @String
(* VER: 1.2 2017-03-03
---------------------------------------------------------------------------------
PURPOSE: Convert Date (w/optional time) String in ISO format to AppleScript Date Type
PARAMETERS:
• pDateStr | text | Date String in ISO format
Assumes local time zone
If time is included, assumed to be 24-HR format
RETURNS: date │ AppleScript date
AUTHOR: JMichaelTX
pDateStr MUST be in the format of
YYYY<delim>MM<delim>DD
OR, if time with minutes is included:
YYYY<delim>MM<delim>DD<AnyChar>HH<delim>MM
OR, if time with seconds is included:
YYYY<delim>MM<delim>DD<AnyChar>HH<delim>MM<delim>SS
in local time.
where <delim> can be any character
like any of these:
2016-01-05
2016-01-05 13:01:15
2016-01-05T13:01:15
2016-01-05 13:01
2016-01-05 13:01:15
—————————————————————————————————————————————————————————————————————————————————
*)
set
resultDate
to
the current date
set
the
day of
resultDate
to
1 ## Thanks to Shane
try
--- ANY/ALL DELIMITERS OF 1 CHAR WILL WORK ---
set the
year of
resultDate to (text
1 thru
4 of pDateStr)
as integer
set the
month of
resultDate to (text
6 thru
7 of pDateStr)
as integer
set the
day of
resultDate to (text
9 thru
10 of pDateStr)
as integer
set the
time of
resultDate to
0
if (length
of
pDateStr) >
10 then
-- include HH:MM
set the
hours of
resultDate to (text
12 thru
13 of pDateStr)
as integer
set the
minutes of
resultDate to (text
15 thru
16 of pDateStr)
as integer
if (length
of
pDateStr) >
16 then
-- include seconds
set the
seconds of
resultDate to (text
18 thru
19 of pDateStr)
as integer
end if
end if
on
error
errMsg number
errNum
error
"[ERROR]" & return &
"INVALID Date Format: " & pDateStr
end try
return
resultDate
end convertISOtoDate
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~