Swift 日期處理類(lèi)庫(kù) Timepiece,可對(duì)日期進(jìn)行加減運(yùn)算、初始設(shè)置、變更、格式化和解析。
示例代碼:
加減
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)