Swift 日期处理类库 Timepiece,可对日期进行加减运算、初始设置、变更、格式化和解析。
示例代码:
加减
let now = NSDate() let nextWeek = now + 1.week let dayAfterTomorrow = now + 2.days // shortcuts #1 let today = NSDate.today() let tomorrow = NSDate.tomorrow() let yesterday = NSDate.yesterday() // shortcuts #2 let dayBeforeYesterday = 2.days.ago let tokyoOlympicYear = 5.years.later格式化和解析
5.minutes.later.stringFromFormat("yyyy-MM-dd HH:mm:SS") //=> "2015-03-01 12:05:00" "1987-06-02".dateFromFormat("yyyy-MM-dd") //=> NSDate.date(year: 1987, month: 6, day: 2)