【问题标题】:How to Deal with multiple time zones in Application? [duplicate]如何处理应用程序中的多个时区? [复制]
【发布时间】:2018-07-20 05:23:30
【问题描述】:

例如。我们需要以哪种格式存储和获取日期?它应该存储在服务器时间吗?还是所有日期都应该存储在特定的一个时区?不知道怎么处理。

【问题讨论】:

  • 您通常会在 UTC 中存储日期,然后您可以调整到设备上的本地时区

标签: ios timezone


【解决方案1】:

这里是将 UTC 转换为设备本地时区的代码。如果您的服务器时区是印度,则使用“IST”缩写而不是“UTC”。

extension String{
func convertUTCToLocalDate(inputFormat: String, outputFormat: String)-> String{
    let formatter = DateFormatter()
        formatter.dateFormat = inputFormat
            formatter.timeZone = TimeZone(abbreviation: "UTC")
    let oldDate = formatter.date(from: self)
    formatter.dateFormat = outputFormat
    formatter.timeZone = TimeZone.current 
    return (oldDate != nil) ? formatter.string(from: oldDate!) : self
}}

用法:-

let localDate = "20 Jul, 2018".convertUTCToLocalDate(inputFormat: "dd MMM, yyyy", outputFormat: "dd MMM, yyyy")

【讨论】:

    猜你喜欢
    • 2017-08-08
    • 2021-12-28
    • 2019-01-20
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    相关资源
    最近更新 更多