• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Calculate Current Year Quarter with Time Zone Using AppleScriptObjC
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calculate Current Year Quarter with Time Zone Using AppleScriptObjC


  • Subject: Re: Calculate Current Year Quarter with Time Zone Using AppleScriptObjC
  • From: Shane Stanley <email@hidden>
  • Date: Sun, 15 Jan 2017 13:19:57 +1100

On 15 Jan 2017, at 1:43 am, Deivy Petrescu <email@hidden> wrote:

return ((((month of (current date)) - 1) div 3 + 1) as string) & " Q”

Except that returns the value for your own time zone. I think Chris's offering, judging by the links, was about getting the value based on a different time zone.

(As it happens, there is an NSCalendarUnitQuarter enum, but the docs say not to use it because it's unimplemented.)

Anyway, here's a slight variation on Chris's method -- one that makes it easier to get other components, and works around some enum quirks:

use framework "Foundation"
use scripting additions

set currentCalendar to current application's NSCalendar's currentCalendar()
set theTimeZone to current application's NSTimeZone's timeZoneWithName:"US/Central"
set theComponents to currentCalendar's componentsInTimeZone:theTimeZone fromDate:(current application's NSDate's |date|())
set currentMonth to theComponents's |month|() -- or |year|(), |day|(), |hour|(), |minute|(), |second|(), nanosecond(), weekdayOrdinal(), weekOfMonth(), weekOfYear(), yearForWeekOfYear(), leapMonth()

And if you want to be locale aware as well, you could use something like this:

set currentCalendar to current application's NSCalendar's currentCalendar()
currentCalendar's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP")
set theTimeZone to current application's NSTimeZone's timeZoneWithName:"Japan/Tokyo"
set theComponents to currentCalendar's componentsInTimeZone:theTimeZone fromDate:(current application's NSDate's |date|())
set currentMonth to theComponents's |month|()
set currentQuarter to (item ((currentMonth - 1) div 3 + 1) of currentCalendar's standaloneQuarterSymbols()) as text
-- or: set currentQuarter to (item ((currentMonth + 2) div 3) of currentCalendar's standaloneQuarterSymbols()) as text
--> "第1四半期"

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Calculate Current Year Quarter with Time Zone Using AppleScriptObjC (From: Christopher Stone <email@hidden>)
 >Re: Calculate Current Year Quarter with Time Zone Using AppleScriptObjC (From: Deivy Petrescu <email@hidden>)

  • Prev by Date: AS Bug with 10.12?
  • Next by Date: Re: Asking a favour, please?
  • Previous by thread: Re: Calculate Current Year Quarter with Time Zone Using AppleScriptObjC
  • Next by thread: NS Keywords
  • Index(es):
    • Date
    • Thread