[swift]時間格式

小黑
發佈於APP
2023/11/08閱讀時間約 1 分鐘

取得當前時間

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)





7會員
78內容數
嗨,我是一名程式設計師,會在這分享開發與學習紀錄。
留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容