Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 从两个日期算起多少天_Date_Applescript - Fatal编程技术网

Date 从两个日期算起多少天

Date 从两个日期算起多少天,date,applescript,Date,Applescript,我需要计算两个日期之间的日期,并告诉我之间有多少天,如果超过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

我需要计算两个日期之间的日期,并告诉我之间有多少天,如果超过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
将X设置为MYdatefromSafari--“2016年8月26日”
将D设置为日期(X)
日志D
ConvertDate(X)——将字符串“english_month dayth/st,year”转换为实际日期的子例程
将MS设置为{“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”}
将LW设置为X的每个单词
如果(LW计数)不是3,则返回“”--格式无效
将MI设置为0--检查月份:应在列表中
从1到12重复I
如果MS的项目I是LW的项目1,则将MI设置为I
结束重复
如果MI为0,则返回“”--第一个单词不在月份列表中
试试看——检查日期:应该是NNst的NNth
将DI设置为整数(LW第2项的文本1至-3)
结束尝试
如果不是((DI>0)和(DI<31)),则返回“”--无效日期
试试看——检查年份
将YI设置为整数(LW第3项)
结束尝试
如果不是((YI>0)和(YI<9999)),则返回“”--无效年份
返回日期((DI&“/”&MI&“/”&YI)作为字符串)
结束日期
在最好的情况下,如果少于一年,则计算日期数;如果超过一年,则计算月份或年份数
编辑:
将X设置为“2016年8月26日”
将MyDate设置为ConvertDate(X)
将D设置为日期(X)
日志D
将SecondDate设置为(当前日期)--=系统日期
将ListDiff设置为DateDiff(D,CD)--返回{diff天,diff月,diff年}
日志“Days=”&ListDiff的第1项
记录“月数=”&ListDiff的第2项
记录“年份=”&ListDiff的第3项
DateDiff(D1,D2)——返回列表,以天、月、年为单位
--取决于差异是小于月、小于年还是大于年
如果D1>D2,则--将DStart设置为最早的日期
将{D1,D2}复制到{Dend,DStart}
其他的
将{D1,D2}复制到{DStart,Dend}
如果结束
返回{(Dend-DStart)分区天数,(Dend-DStart)分区(30*天),(Dend-DStart)分区(365*天)}
结束日期差
ConvertDate(X)——将字符串“english_month dayth/st,year”转换为实际日期的子例程
将MS设置为{“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”}
将LW设置为X的每个单词
如果(LW计数)不是3,则返回“”--格式无效
将MI设置为0--检查月份:应在列表中
从1到12重复I
如果MS的项目I是LW的项目1,则将MI设置为I
结束重复
如果MI为0,则返回“”--第一个单词不在月份列表中
试试看——检查日期:应该是NNst的NNth
将DI设置为整数(LW第2项的文本1至-3)
结束尝试
如果不是((DI>0)和(DI<31)),则返回“”--无效日期
试试看——检查年份
将YI设置为整数(LW第3项)
结束尝试
如果不是((YI>0)和(YI<9999)),则返回“”--无效年份
返回日期((DI&“/”&MI&“/”&YI)作为字符串)
结束日期
下面的子程序“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年两个日期!)。

看起来不错,谢谢。不过我有一个错误,变量CD没有定义,我用完整的脚本编辑了原始文章