我需要计算两个日期之间的日期,并告诉我两天之间的日期,如果超过30天,我会针对某些日期。
在这个脚本中,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
答案 0 :(得分:1)
子程序“DateDiff”下面列出了3个不同的值:天,月和年。
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
例如,如果MyDate = 2016年1月20日且我们是2016年8月26日,它将返回{219,7,0},因为差异是216天或7个月(1月至8月)或0年(2016年两个日期!)