2016-08-26 33 views
0

私は2つの日付の間の日付を計算する必要があります。私は今日から計算したい、このスクリプトでAppleScript:2つの日付から何日を計算するか

Dは、(過去の)日付で

set X to MYdatefromSafari -- "August 26th, 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 


In the best scenario, that would calculate the number of date in between if less than a year, and month or year if more 


EDIT : 
set X to "August 26th, 2016" 
set MyDate to ConvertDate(X) 

set D to ConvertDate(X) 
log D 

set SecondDate to (current date) -- = system date 
set ListDiff to DateDiff(D, CD) -- returns {diff days, diff months, diff years} 
log "Days = " & item 1 of ListDiff 
log "Months = " & item 2 of ListDiff 
log "Years = " & item 3 of ListDiff 

on DateDiff(D1, D2) -- return list with difference in days, in months, in years 
    -- depending if differences is less than month, or less than year or higher than a year 
    if D1 > D2 then -- set DStart as oldest date 
     copy {D1, D2} to {Dend, DStart} 
    else 
     copy {D1, D2} to {DStart, Dend} 
    end if 
    return {(Dend - DStart) div days, (Dend - DStart) div (30 * days), (Dend - DStart) div (365 * days)} 
end DateDiff 

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 

答えて

1

サブルーチン「DateDiff関数」怒鳴るは3つの差分値のリストをバック与える:日、月にそして年。 MyDateに該当=月20日2016年、我々は8月26日2016年であれば、それは{219、7、0}差が216日または7ヶ月(8月-1月)または0年であるため、(2016例えば

Set X to MyDatefrom Safari 
Set MyDate to ConvertDate(X) 

set SecondDate to (current date) -- = system date 
set ListDiff to DateDiff(D, CD) -- returns {diff days, diff months, diff years} 
log "Days = " & item 1 of ListDiff 
log "Months = " & item 2 of ListDiff 
log "Years = " & item 3 of ListDiff 

on DateDiff(D1, D2) -- return list with difference in days, in months, in years 
-- depending if differences is less than month, or less than year or higher than a year 
if D1 > D2 then -- set DStart as oldest date 
    copy {D1, D2} to {Dend, DStart} 
else 
    copy {D1, D2} to {DStart, Dend} 
end if 
return {(Dend - DStart) div days, (Dend - DStart) div (30 * days), (Dend - DStart) div (365 * days)} 
end DateDiff 

on ConvertDate(X) -- copy your existing sub-routine 
end ConvertDate 

を返します。両方の日付!)。

+0

それはよく見えます、ありがとうございます。私はエラーがありますが、変数CDは定義されていません、私は完全なスクリプトでオリジナルの投稿を編集しました –

関連する問題