[英]Represent date string as UTC date and convert it into local timezone
后端為我提供了格式為“ 2018-09-07 01:00:00 am”的日期,該日期的時區為UTC。 現在,我必須將其轉換為本地時區。 我正在使用的功能是:
func getLocalDate(dateString: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss a"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.locale = Locale.current
let dt = dateFormatter.date(from: dateString)
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss aZ"
return dateFormatter.string(from: dt!)
}
有了這個,日期會先轉換為UTC,然后再轉換為本地時區,所以我沒有得到想要的結果。 如何將該日期的時區設置為UTC,而不進行任何轉換?
編輯:我得到的輸出是2018-09-07 05:45:00 AM + 0545但預期的輸出是2018-09-07 06:45:00 AM + 0545
12小時制的格式說明符是hh
,而不是24小時制的HH
。
將HH
與am / pm說明符a
結合使用將產生錯誤的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.