Convert NSDate to String with a specific timezone in SWIFT

前端 未结 1 1608
萌比男神i
萌比男神i 2021-01-06 09:56

In my coredatabase I have an \"news\" entity with a NSDate attribute. My app is worldwide.

News was published 2015-09-04 22:15:54 +0000 French hour

1条回答
  •  既然无缘
    2021-01-06 10:16

    Xcode 8 beta • Swift 3.0

    Your ISO8601 date format is basic hms without Z. You need to use "xxxx" for "+0000".

    "+0000" means UTC time.

    let dateString = "2015-09-04 22:15:54 +0000"
    
    let dateFormatter = DateFormatter()
    dateFormatter.calendar = Calendar(calendarIdentifier: .ISO8601)
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss xxxx"
    dateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
    if let dateToBeSaved = dateFormatter.date(from: dateString) {
        print(dateToBeSaved)   // "2015-09-04 22:15:54 +0000"
    }
    

    If you need some reference to create your date format you can use this:

    0 讨论(0)
提交回复
热议问题