This have been done before but maybe will be useful to new people to ASObjC.
Class: NSDateFormatter
use framework "Foundation"
use scripting additions
(**
* Reference:
* https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DataFormatting/DataFormatting.html
*
* https://developer.apple.com/documentation/foundation/nsdateformatter
*
* https://nsdateformatter.com
*)
(**
* [YEAR]
* y, 2008, Year no padding
* yy, 08, Year two digits (padding with a zero if necessary)
* yyyy 2008, Year minimum of four digits (padding with zeros if necessary)
*)
(**
* [QUARTER]
* Q, 4, The quarter of the year. Use QQ if you want zero padding.
* QQQ, Q4, Quarter including "Q"
* QQQQ, 4th quarter, Quarter spelled out
*)
(**
* [MONTH]
* M, 12, The numeric month of the year. A single M will use '1' for January.
* MM , 12, The numeric month of the year. A double M will use '01' for January.
* MMM, Dec, The shorthand name of the month
* MMMM, December, Full name of the month
* MMMMM, D, Narrow name of the month
*)
(**
* [DAY]
* d, 14, The day of the month. A single d will use 1 for January 1st.
* dd , 14, The day of the month. A double d will use 01 for January 1st.
* F, 3rd Tuesday in December, The day of week in the month
* E, Tue, The abbreviation for the day of the week
* EEEE, Tuesday, The wide name of the day of the week
* EEEEE, T, The narrow day of week
* EEEEEE, Tu, The short day of week
*)
(**
* [HOUR]
* h, 4, The 12-hour hour.
* hh , 04, The 12-hour hour padding with a zero if there is only 1 digit
* H, 16, The 24-hour hour.
* HH , 16, The 24-hour hour padding with a zero if there is only 1 digit.
* a, PM, AM / PM for 12-hour time formats
*)
(**
* [MINUTE]
* m, 35, The minute, with no padding for zeroes.
* mm , 35, The minute with zero padding.
*)
(**
* [SECOND]
* s, 8, The seconds, with no padding for zeroes.
* ss , 08, The seconds with zero padding.
* SSS, 123, The milliseconds.
*)
(**
* [TIME ZONE]
* zzz, CST, The 3 letter name of the time zone. Falls back to GMT-08:00 (hour offset)
* zzzz, Central Standard Time, The expanded time zone name, falls back to GMT-08:00
* ZZZZ, CST-06:00, Time zone with abbreviation and offset
* Z, -0600, RFC 822 GMT format. Can also match a literal Z for Zulu (UTC) time.
* ZZZZZ, -06:00, ISO 8601 time zone format
*)
-- Examples.
log (my dateFormatterWithFormat:"EEEE, MMM d, yyyy")
log (my dateFormatterWithFormat:"MM/dd/yyyy")
log (my dateFormatterWithFormat:"MM-dd-yyyy HH:mm")
log (my dateFormatterWithFormat:"MMM d, h:mm a")
log (my dateFormatterWithFormat:"MMMM yyyy")
log (my dateFormatterWithFormat:"MMM d, yyyy")
log (my dateFormatterWithFormat:"E,d MMM yyyy HH:mm:ss Z")
log (my dateFormatterWithFormat:"yyyy-MM-dd'T'HH:mm:ssZ")
log (my dateFormatterWithFormat:"dd.MM.yy")
log (my dateFormatterWithFormat:"HH:mm:ss")
log (my dateFormatterWithFormat:"MMMM dd, yyyy HH:mm")
if ((my dateFormatterWithFormat:"F") = "1") then
log (my dateFormatterWithFormat:"F'st' EEEE 'in the month'")
end if
if ((my dateFormatterWithFormat:"F") = "2") then
log (my dateFormatterWithFormat:"F'nd' EEEE 'in the month'")
end if
if ((my dateFormatterWithFormat:"F") = "3") then
log (my dateFormatterWithFormat:"F'rd' EEEE 'in the month'")
end if
if ((my dateFormatterWithFormat:"F") = "4") then
log (my dateFormatterWithFormat:"F'th' EEEE 'in the month'")
end if
(**
* [Class]: NSDateFormatter
* A formatter that converts between dates and their textual representations.
*)
(**
* [dateFormatterWithFormat:(string)]
*)
on dateFormatterWithFormat:_dateFormat
set formatter to current application's NSDateFormatter's alloc()'s init()
formatter's setDateFormat:(_dateFormat as string)
set theString to (formatter's stringFromDate:(current date)) as text
end dateFormatterWithFormat: