【发布时间】:2023-04-06 13:20:01
【问题描述】:
根据UTC to IST conversion tool,如果 UTC 12Hr 日期字符串为 = "2018-12-31 07:35 PM",则需要将其转换为 "2019-01-01 01:05"(IST 中的 24Hr 格式)[注意:此处 12Hr IST 转换日期为“2019-01- 01 01:05 AM"]
那么如何将这个 12Hr UTC 日期转换为 24Hr IST 日期格式呢?
当前代码是:
static func convertUTCdateToLocalTimezoneDate(dateString: String, fromDateFormat: String, toDateFormat: String) -> String {
if dateString.count > 0 {
let dateFormatter = DateFormatter()
let is24Hr: Bool = AppUtility.isSystemTimeFormatIs24Hr(dateString: AppUtility.getIphoneCurrnetTime())
let fromDateFormatLocal = is24Hr == true ? kDateIn24Format : kDateIn12Format
dateFormatter.dateFormat = fromDateFormatLocal
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let date = dateFormatter.date(from: dateString)
var timeStamp = ""
dateFormatter.dateFormat = toDateFormat
dateFormatter.timeZone = NSTimeZone.local
timeStamp = dateFormatter.string(from: date!)
return timeStamp
}
return ""
}
static func isSystemTimeFormatIs24Hr(dateString: String) -> Bool {
let formatter = DateFormatter()
formatter.locale = NSLocale.current
formatter.dateStyle = .none
formatter.timeStyle = .short
let amRange: NSRange? = (dateString as NSString).range(of: formatter.amSymbol)
let pmRange: NSRange? = (dateString as NSString).range(of: formatter.pmSymbol)
let is24h: Bool = amRange?.location == NSNotFound && pmRange?.location == NSNotFound
return is24h
}
【问题讨论】:
标签: ios swift date timezone nsdateformatter