取得當前時間
let currentDate = NSDate()
設定時間格式
樣式一:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.init(identifier: "zh_Hant_TW")
dateFormatter.timeZone = TimeZone.init(identifier: "Asia/Taipei")
dateFormatter.calendar = Calendar.init(identifier: .chinese)
dateFormatter.dateFormat = "U年 M月 d日 hh時mm分ss秒"//顯示農曆年
var convertedDate = dateFormatter.string(from: currentDate as Date)
樣式二:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.init(identifier: "zh_Hant_TW")
dateFormatter.timeZone = TimeZone.init(identifier: "Asia/Taipei")
dateFormatter.calendar = Calendar.init(identifier: .gregorian)
dateFormatter.dateFormat = "yyyy-M-d hh:mm:ss.SSS"
var convertedDate = dateFormatter.string(from: currentDate as Date)