欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Swift中格式化日期

程序员文章站 2024-02-23 09:56:28
...

        Swift语言中格式化日期跟其它编程语言很相似:

        

var dformatter = NSDateFormatter()

dformatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"

var datestr = dformatter.stringFromDate(date)

 用Calendar操作日期 

let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)

let now = NSDate()

var dateformatter = NSDateFormatter()
dateformatter.dateFormat = "yyyy-MM-dd"
let birthday = dateformatter.dateFromString("1986-11-10")

let components = gregorian?.components(NSCalendarUnit.CalendarUnitYear, fromDate: birthday!, toDate: now, options: NSCalendarOptions(0))

components?.year