2016-08-19 5 views
0

一つは、この形式の日付である「8月7日、2016年」のAppleScript再フォーマット日付

私のシステムの日付は、私はAppleScriptの日付を変更し、計算することができますどのようにDD/MM/YY

ですこれは今日から何日ですか?

種類が

編集(日付convertion)

set X to set {year:y, month:m, day:d} to (current date) 

set convertedTime to m & d & y 

または

set {year:y, month:m, day:d} to (current date) 

set convertedTime to m & " " & d & "th," & " " & y 

についてアップデート:私はWhaever、私は無効な日付と時刻日付6月3日に

を持っています、 10.11がアップルスクリプトをアップデートしたためでしょうか?

tell (date "june 3, 2006") to get its month as integer 

答えて

1

「2016年8月7日」の日付がすべて地域の日付設定と一致しない場合、スクリプトはその日付を解析して各用語を変換しています。もちろん、地域の日付設定と一致させる方がはるかに効率的です!

-- date conversion without taking care of regional settings 
-- assume date in in English format and returned format will be dd/mm/yy (not mm/dd/yy) 
set X to "August 7th, 2016" 

set D to ConvertDate(X) 
log D 


on ConvertDate(X) -- sub routine to convert string "english_month dayth/st, year" to real date 
set MS to {"January", February, "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} 
set LW to every word of X 
if (count of LW) is not 3 then return "" -- invalid format 
set MI to 0 -- check month : should be in the list 
repeat with I from 1 to 12 
    if item I of MS is item 1 of LW then set MI to I 
end repeat 
if MI is 0 then return "" -- the fisrt word is not in the list of months  
try -- check day : it should be NNth of NNst 
    set DI to (text 1 thru -3 of item 2 of LW) as integer 
end try 
if not ((DI > 0) and (DI < 31)) then return "" -- invalid day 
try -- check year 
    set YI to (item 3 of LW) as integer 
end try 
if not ((YI > 0) and (YI < 9999)) then return "" -- invalid year 
return date ((DI & "/" & MI & "/" & YI) as string) 
end ConvertDate 
+0

「8月7日、2016年」というのはウェブサイトからのものです(私のコンピュータはサファリからのものです)。実際には私のコンピュータは実際には 私は「172800のすべての単語を得ることはできません」という172800のすべての単語から-1728の数字を得ることができない日を追加または削除しようとする瞬間に、感謝s –

0

あなたは最初の日にあなたの日付/文字列フォーマットをcoerc必要があります。そして、あなたはシステムの現在の日付との差を作る - >あなたは「DIV」演算子を使用して「日」で割るの秒数を取得:

set X to date "August 7th, 2016" 
set Delta to (((current date) - X) as integer) div days 
log Delta 

あなたの日付は8月7日であり、我々は8月19ですが、その後、デルタは次のようになります12日!

+0

ご回答いただきありがとうございます。コードは「2016年8月7日の日付と日付が無効です」と表示されています。私の日付変換が無効になることを期待しています(スクリプトを編集しました) –

+0

文字列の日付を実際の日付に強制するには、文字列が地域設定(システム設定/言語と地域/高度/日付) 2016年はイギリスの設定に従っているはずです(私は思っています!)そういう場合は、これらの設定でどこか他の場所からその日付を取得していると思います。 – pbell