AppleScript重新格式化日期

时间:2016-08-19 09:22:40

标签: date format applescript

我的变量之一是这种格式的日期" 2016年8月7日"

我的系统日期是DD / MM / YY

如何更改AppleScript日期,然后计算从今天起的这一天?

亲切的问候

编辑(日期转换)

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

更新:

无论我做什么,我都有无效的日期和时间日期2006年6月3日。那会不会是因为10.11更新了AppleScript?

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您必须先将日期/字符串格式强制转换为日期。然后你与系统当前日期有所不同 - &gt;获得除以&#34;天数&#34;使用&#34; div&#34;运营商:

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

您的日期是8月7日,我们是8月19日,那么Delta将是12天!

相关问题