Archive for the 'NSDateIntervalFormatter' Category

2016/01/14 ローカライズドな期間表記

Cocoaの機能を用いて、ローカライズされた(英語、日本語などの言語環境を考慮した)期間表示を行うAppleScriptです。

それほど気の利いた表記をしてくれるわけではないのと、日本語環境だとShortでもMidiumでもLongでも表記内容が変わりません。

  2016/1/14〜27

のように、開始日と修了日の年や月に変更がなければ、それを記載しないといった「気の利かせ方」を発揮していただきたいものであります。

AppleScript名:ASOCでローカライズドな期間表記
– Created 2016-01-14 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

–Date Interval

set engNSDateIntervalFormatter to current application’s NSDateIntervalFormatter’s alloc()’s init()
engNSDateIntervalFormatter’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:“en-US”)
engNSDateIntervalFormatter’s setDateStyle:(current application’s NSDateIntervalFormatterLongStyle)
engNSDateIntervalFormatter’s setDateTemplate:(current application’s NSDateFormatter’s alloc()’s init()’s setDateFormat:“yyyy-MM-dd HH:mm”)
engNSDateIntervalFormatter’s setTimeStyle:(current application’s NSDateIntervalFormatterNoStyle)
engNSDateIntervalFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithAbbreviation:“JST”)
engNSDateIntervalFormatter’s setCalendar:(current application’s NSCalendar’s currentCalendar())

set startDate to current application’s NSDate’s |date|()
set endDate to current application’s NSDate’s dateWithTimeInterval:86400 sinceDate:startDate
set outEString to (engNSDateIntervalFormatter’s stringFromDate:startDate toDate:endDate) as string
–>  ”1/14/16 - 1/15/16″–ShortStyle
–>  ”Jan 14 - 15, 2016″–Medium Style
–>  ”January 14 - 15, 2016″–Long Style
–>  ”Thursday, January 14 - Friday, January 15, 2016″–Full Style

set jpnNSDateIntervalFormatter to current application’s NSDateIntervalFormatter’s alloc()’s init()
jpnNSDateIntervalFormatter’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:“ja”)
jpnNSDateIntervalFormatter’s setDateStyle:(current application’s NSDateIntervalFormatterFullStyle)
jpnNSDateIntervalFormatter’s setDateTemplate:(current application’s NSDateFormatter’s alloc()’s init()’s setDateFormat:“yyyy-MM-dd HH:mm”)
jpnNSDateIntervalFormatter’s setTimeStyle:(current application’s NSDateIntervalFormatterNoStyle)
jpnNSDateIntervalFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithAbbreviation:“JST”)
jpnNSDateIntervalFormatter’s setCalendar:(current application’s NSCalendar’s currentCalendar())

set startDate to current application’s NSDate’s |date|()
set endDate to current application’s NSDate’s dateWithTimeInterval:86400 sinceDate:startDate
set outJString to (jpnNSDateIntervalFormatter’s stringFromDate:startDate toDate:endDate) as string
–>  ”2016/01/14〜2016/01/15″–ShortStyle
–>  ”2016/01/14〜2016/01/15″–Medium Style
–>  ”2016/01/14〜2016/01/15″–Long Style
–>  ”2016/01/14(木曜日)〜2016/01/15(金曜日)”–Full Style

★Click Here to Open This Script