You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Apple Mail邮件接收日期的月份转为数字并生成短日期格式

解决Mail脚本中日期格式转换的问题

嘿,这个问题其实很容易搞定——AppleScript里的month对象本身就支持直接转换为数字格式,不用手动去做英文月份到数字的映射!

完整的代码修改示例

tell application "Mail"
    repeat with aMessage in messages of inbox
        set sSender to (get aMessage's sender)
        set recDate to (date received of aMessage)
        
        -- 将月份转换为数字
        set monthNum to month of recDate as integer
        -- 获取日期
        set dayNum to day of recDate
        -- 获取两位年份(取完整年份的最后两位)
        set yearShort to text -2 thru -1 of (year of recDate as string)
        
        -- 拼接成目标格式 "4/2/17"
        set formattedDate to monthNum & "/" & dayNum & "/" & yearShort
        
        -- 这里可以添加写入文本文件的代码,比如:
        -- set filePath to "/path/to/your/file.txt"
        -- set fileRef to open for access filePath with write permission
        -- write formattedDate & linefeed to fileRef starting at eof
        -- close access fileRef
    end repeat
end tell

关键步骤解释

  • 月份转数字:AppleScript的month类型不是单纯的字符串,它是一个内置的枚举值,直接用as integer就能把April转换成4,其他月份同理(January→1,February→2...)。
  • 两位年份:通过把完整年份转成字符串后截取最后两位,就能得到17这种格式。
  • 格式拼接:直接把数字月份、日期、两位年份用/连接起来,就得到了你想要的MM/DD/YY格式。

如果需要处理日期或月份小于10时是否补零的情况(比如把4/2/17改成04/02/17),可以加个简单的判断:

-- 处理月份补零
set monthStr to (monthNum as string)
if length of monthStr is 1 then set monthStr to "0" & monthStr

-- 处理日期补零
set dayStr to (dayNum as string)
if length of dayStr is 1 then set dayStr to "0" & dayStr

set formattedDate to monthStr & "/" & dayStr & "/" & yearShort

这样就能得到更规范的两位数字格式啦!

内容的提问来源于stack exchange,提问作者Steve Gon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:15:51