【问题标题】:Ios Swift Date Convert from TimeZoneIos Swift 日期从 TimeZone 转换
【发布时间】:2018-07-24 06:11:59
【问题描述】:
 dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    dateFormatter.timeZone = TimeZone(abbreviation: "KST")
    let nowstring = dateFormatter.string(from: Date())
    let nowdate = dateFormatter.date(from: nowstring)
    print(nowstring)
    print(nowdate)

打印这个2018-07-24T15:06:26 Optional(2018-07-24 06:06:26 +0000)

我想要 nowdate 值2018-07-24T15:06:26

但是没有时区的字符串到日期

我必须使用 timeIntervalSince 所以 nowdate 必须有时区

【问题讨论】:

    标签: ios swift date


    【解决方案1】:
    let today = NSDate()
    print("\(today)")
    
    
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    let nowDatestring = formatter.string(from: today)
    
    formatter.timeZone = TimeZone(abbreviation: "KST")
    let kstDate = formatter.date(from: nowDatestring)
    print("\(kstDate!)")
    
    
    formatter.timeZone = TimeZone(abbreviation: "PST")
    let pstDate = formatter.date(from: nowDatestring)
    print("\(pstDate!)")
    
    
    formatter.timeZone = TimeZone(abbreviation: "IST")
    let istDate = formatter.date(from: nowDatestring)
    print("\(istDate!)")
    

    【讨论】:

      【解决方案2】:

      Date 是一个特定的时间点,与时区无关。如果您打印日期值,它将始终独立于任何日历或时区。它将为你(KST)和我(IST)提供相同的价值。您可以在使用DateFormatter 对其进行格式化时指定您的时区。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-03
        • 1970-01-01
        相关资源
        最近更新 更多