[ SWIFT ] - 日期格式 - dateFormatter - timeStyle & dateStyle

let now = NSDate() // 取的現在時間
let dateFormatter = NSDateFormatter()

dateFormatter.locale = NSLocale(localeIdentifier: "en_US") // 地區時間設定

dateFormatter.timeStyle = .NoStyle // 時間格式設定
dateFormatter.dateStyle = .NoStyle // 日期格式設定
println(dateFormatter.stringFromDate(now)) // -> 執行結果

dateFormatter.timeStyle = .ShortStyle
dateFormatter.dateStyle = .ShortStyle
println(dateFormatter.stringFromDate(now)) // -> 6/24/14, 11:01 AM

dateFormatter.timeStyle = .MediumStyle
dateFormatter.dateStyle = .MediumStyle
println(dateFormatter.stringFromDate(now)) // -> Jun 24, 2014, 11:01:31 AM

dateFormatter.timeStyle = .LongStyle
dateFormatter.dateStyle = .LongStyle
println(dateFormatter.stringFromDate(now)) // -> June 24, 2014 at 11:01:31 AM GMT+9

dateFormatter.timeStyle = .FullStyle
dateFormatter.dateStyle = .FullStyle
println(dateFormatter.stringFromDate(now)) // -> Tuesday, June 24, 2014 at 11:01:31 AM Japan Standard Time

dateFormatter.timeStyle = .NoStyle // 不顯示時間
dateFormatter.dateStyle = .FullStyle
println(dateFormatter.stringFromDate(now)) // -> Tuesday, June 24, 2014

留言

熱門文章