【问题标题】:Convert 12Hr UTC date to 24Hr Local TimeZone format in Swift在 Swift 中将 12 小时 UTC 日期转换为 24 小时本地时区格式
【发布时间】: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


    【解决方案1】:

    以下代码将解决您的问题

        func dateFormatter() -> Date? {
            let inputDateString = "2018-12-31 07:30:00 PM"
    
            let outFormatter = DateFormatter()
            outFormatter.locale = Locale(identifier: "UTC")
            outFormatter.timeZone = TimeZone(abbreviation: "UTC")
            outFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss a"
    
            let outDate = outFormatter.date(from: inputDateString)
    
            outFormatter.locale = Locale.current
            outFormatter.timeZone = TimeZone.current
            outFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
    
            let date12HrsString = outFormatter.string(from: outDate!)
            print("12 Hrs Format: \(date12HrsString)")
    
            outFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            let date24HrsString = outFormatter.string(from: outDate!)
            print("24 Hrs Format: \(date24HrsString)")
    
            return outDate
        }
    

    【讨论】:

      【解决方案2】:
      func getDateFrom(_ dateString: String, withFormat formatString: String, timeZone: String = "UTC", locale: String = "UTC") -> Date? {
          var dateToReturn: Date?
          let formatter = DateFormatter()
          formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
          formatter.locale = Locale(identifier: locale)
          formatter.dateFormat = formatString
          dateToReturn = formatter.date(from: dateString)
          return dateToReturn
      }
      
      func getStringFrom(_ date: Date, withFormat formatString: String, timeZone: String = "UTC", locale: String = "UTC") -> String? {
          var stringToReturn: String?
          let formatter = DateFormatter()
          formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
          formatter.locale = Locale(identifier: locale)
          formatter.dateFormat = formatString
          stringToReturn = formatter.string(from: date)
      
          return stringToReturn
      }
      let string12H = "2018-12-31 07:35 PM"
      
      if let date = getDateFrom(string12H, withFormat: "yyyy-MM-dd hh:mm a") {
          let dateStringWith12HFormat = getStringFrom(date, withFormat: "yyyy-MM-dd hh:mm a", timeZone: "IST", locale: "IST")
          let dateStringWith24HFormat = getStringFrom(date, withFormat: "yyyy-MM-dd HH:mm", timeZone: "IST", locale: "IST")
      }
      

      【讨论】:

      • 如果我的答案需要改进,请告诉我。
      • 提供的解决方案不适用于 24Hr TimeZone。输入日期字符串为 12Hr UTC,我们需要将其转换为 24Hr Local TimeZone 格式。
      • 仍然无法工作 - 从设置 - 常规 - 日期和时间更新设备时钟格式并打开 24 小时制并尝试。
      • @SuvaP 我已经根据您的要求更新了我的答案重新检查。
      猜你喜欢
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多