2016-01-20 191 views
0

我想要一個從當前時間倒數到鬧鐘時間的字符串。AppleScript倒計時

我已經設法弄清楚如何獲取當前時間以及如何設置鬧鐘時間。

我遇到的問題是,當我將當前時間 - 鬧鐘時間給了我一個數字女巫,然後我需要格式化回hh:mm:ss字符串。

我有這個。

set alarmHour to 23 
set alarmMinute to 00 

set theDate to the current date 
set the hours of theDate to alarmHour 
set the minutes of theDate to alarmMinute 
set the seconds of theDate to 0 
theDate 

set countdown to theDate - (current date) 
set ss to countdown/60 

在這一點上給了我22.283333333333巫婆我現在需要轉換爲HH:MM:SS,然後把它們變成給我00:22:00

更新刺痛: 在迅速,你有%你可以使用

countDownTime = (formatterInteger - timeControlInteger) 
    let interval = Int(countDownTime) 
    let seconds  = interval % 60 
    let minutes  = (interval/60) % 60 
    let hours  = (interval/3600) 

但你怎麼做到這一點在蘋果?

+0

在AppleScript的整數除法忽略其餘('/')是'div',模運算符'%'是'mod' – vadian

+0

有沒有一種方法可以像swift一樣格式化字符串?像String(格式:「%02d」,absHour) –

+0

簡短回答:不可以。您必須「手動」執行所有操作。 AppleScript是一種腳本而不是編程語言。 – vadian

回答

0

答到第二個問題:

是有辦法格式字符串像迅速?像 字符串(格式爲: 「%02D」,absHour) - 馬蒂亞斯·海倫

是的,但你需要使用Satimage.osax腳本此外,免費提供的: Satimage AppleScript Additions

Satimage strftime() -- Date/Time Format Function

strftime v:使用類似於C 函數strftime中的規範字符串格式化日期。

的strftime日期或日期

列表插入字符串:格式化字符串。要獲得ISO 8601日期,請使用 「%FT%TZ」或「%GW%V-%uT%TZ」(使用'with GMT'參數)

[GMT布爾值]:如果爲true,格林威治標準時間。默認:假,輸出 日期是本地的。

→字符串:格式化的日期

實施例:的strftime(當前日期)爲「%X」返回:14年7月22日

"%a, %b %d, %Y" RETURNS: Tue, Jul 22, 2014 set d to current date -- some ISO 8601 formats: strftime d into "%FT%T%z" -- "2007-01-15T16:10:56+0100" strftime d into "%GW%V-%uT%T%z" -- "2007W03-1T16:10:56+0100" --if you need to store the date d as UTC: strftime d into "%FT%TZ" with GMT -- "2007-01-15T15:10:56Z" strftime d into "%a, %b %d, %Y %H:%M:%S %z" -- "Mon, Jan 15, 2007 16:10:56 +0100"