2015-04-01 15 views
31

Dostaję z mojego serwera datę w strefie czasowej UTC i muszę ją przekonwertować na lokalną strefę czasową.Konwertowanie formatu daty UTC na lokalny nsdate

mój kod:

let utcTime = "2015-04-01T11:42:00.269Z"  
let dateFormatter = NSDateFormatter() 
dateFormatter.timeZone = NSTimeZone(name: "UTC") 
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" 
let date = dateFormatter.dateFromString(utcTime) 
println("utc: \(utcTime), date: \(date)") 

Drukuje -

UTC: 2015-04-01T11: 42: 00.269Z, data: Opcjonalnie (1.04.2015 11:42:00 +0000)

jeśli usunąć

dateFormatter.timeZone = NSTimeZone(name: "UTC") 

drukuje

utc: 2015-04-01T11: 42: 00.269Z, data: Opcjonalnie (2015-04-01 08:42:00 +0000)

moja lokalna strefa czasowa to UTC +3 iw pierwszej opcji mam UTC w drugim wariancie I dostać UTC -3

powinienem dostać

UTC: 2015-04-01T11: 42: 00.269Z, data: Opcjonalnie (01.04.2015 14:42: 00 +0000)

Jak przekonwertować format daty UTC na czas lokalny?

+0

Czy próbowałeś formatter.timeZone = NSTimeZone.localTimeZone() – boidkan

+0

tak, otrzymuję UTC -3 ... utc: 2015-04-01T11: 42: 00.269Z, data: Opcjonalnie (2015-04-01 08:42:00 +0000) – ilan

+1

http: // stackoverflow .com/questions/23564769/convert-utc-to-local-time-in-objective-c – boidkan

Odpowiedz

56

Coś wzdłuż następujące pracował dla mnie w Objective-C:

// create dateFormatter with UTC time format 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 
NSDate *date = [dateFormatter dateFromString:@"2015-04-01T11:42:00"]; // create date from string 

// change to a readable time format and change to local time zone 
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy - h:mm a"]; 
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; 
NSString *timestamp = [dateFormatter stringFromDate:date]; 

trzymam te dwie strony przydatny do konwersji różnych formatów czasu: http://www.w3.org/TR/NOTE-datetime

http://benscheirman.com/2010/06/dealing-with-dates-time-zones-in-objective-c/

w Swift to będzie być:

// create dateFormatter with UTC time format 
let dateFormatter = NSDateFormatter() 
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" 
dateFormatter.timeZone = NSTimeZone(name: "UTC") 
let date = dateFormatter.dateFromString("2015-04-01T11:42:00")// create date from string 

// change to a readable time format and change to local time zone 
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a" 
dateFormatter.timeZone = NSTimeZone.localTimeZone() 
let timeStamp = dateFormatter.stringFromDate(date!) 
+0

To jest złoto, dziękuję – po5i

+8

W swift3, odpowiednik będzie następujący: '' 'dateFormatter.timezone = TimeZone.current' '' – KingChintz

+0

let date = dateFormatter.dateFromString ("2015-04-01T11: 42: 00") Przypisuje zero do daty, gdy ustawiam 12 godzin w formacie urządzenia. –

24

Swift wersja c_rath odpowiedź:

// create dateFormatter with UTC time format 
let dateFormatter = NSDateFormatter() 
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" 
dateFormatter.timeZone = NSTimeZone(name: "UTC") 
let date = dateFormatter.dateFromString("2015-04-01T11:42:00") 

// change to a readable time format and change to local time zone 
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a" 
dateFormatter.timeZone = NSTimeZone.localTimeZone() 
let timeStamp = dateFormatter.stringFromDate(date!) 
+1

Wiersz 5 powinien brzmieć: dateFormatter.dateFormat = "EEE, MMM d, rrrr - h: mm a", a wiersz 6 powinien brzmieć: dateFormatter.timeZone = NSTimeZone.localTimeZone() – dotToString

+0

To świetnie, dziękuję! – rilar

4

Rozszerzając co wspominają inni, tu jest poręczny NSDate rozszerzenie w Swift

import Foundation 

extension NSDate { 
    func ToLocalStringWithFormat(dateFormat: String) -> String { 
     // change to a readable time format and change to local time zone 
     let dateFormatter = NSDateFormatter() 
     dateFormatter.dateFormat = dateFormat 
     dateFormatter.timeZone = NSTimeZone.localTimeZone() 
     let timeStamp = dateFormatter.stringFromDate(self) 

     return timeStamp 
    } 
} 
3

Może spróbuj coś takiego:

extension NSDate { 
    convenience init(utcDate:String, dateFormat:String="yyyy-MM-dd HH:mm:ss.SSS+00:00") { 
     // 2016-06-06 00:24:21.164493+00:00 
     let dateFormatter = NSDateFormatter() 
     dateFormatter.dateFormat = dateFormat 
     dateFormatter.timeZone = NSTimeZone(name: "UTC") 

     let date = dateFormatter.dateFromString(utcDate)! 
     let s = NSTimeZone.localTimeZone().secondsFromGMTForDate(date) 
     let timeInterval = NSTimeInterval(s) 

     self.init(timeInterval: timeInterval, sinceDate:date) 
    } 
} 
6

W Swift 3 działa to dobrze:

// create dateFormatter with UTC time format 
let dateFormatter = DateFormatter() 
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" 
dateFormatter.timeZone = TimeZone(abbreviation: "UTC") 
let date = dateFormatter.date(from: "2015-04-01T11:42:00")// create date from string 

// change to a readable time format and change to local time zone 
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a" 
dateFormatter.timeZone = TimeZone.current 
let timeStamp = dateFormatter.string(from: date!) 
1

Musiałem usunąć zarówno z, jak i milli sekund od rozwiązania c_rath. Pracuje w szybkim 4.

extension String { 
    func fromUTCToLocalDateTime() -> String { 
     let dateFormatter = DateFormatter() 
     dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" 
     dateFormatter.timeZone = TimeZone(abbreviation: "UTC") 
     var formattedString = self.replacingOccurrences(of: "Z", with: "") 
     if let lowerBound = formattedString.range(of: ".")?.lowerBound { 
      formattedString = "\(formattedString[..<lowerBound])" 
     } 

     guard let date = dateFormatter.date(from: formattedString) else { 
      return self 
     } 

     dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a" 
     dateFormatter.timeZone = TimeZone.current 
     return dateFormatter.string(from: date) 
    } 
} 
1

Spróbuj tego szybkiego rozszerzenia

Swift 4: UTC/GMT ⟺ lokalne (Current/system)

extension Date { 

    // Convert local time to UTC (or GMT) 
    func toGlobalTime() -> Date { 
     let timezone = TimeZone.current 
     let seconds = -TimeInterval(timezone.secondsFromGMT(for: self)) 
     return Date(timeInterval: seconds, since: self) 
    } 

    // Convert UTC (or GMT) to local time 
    func toLocalTime() -> Date { 
     let timezone = TimeZone.current 
     let seconds = TimeInterval(timezone.secondsFromGMT(for: self)) 
     return Date(timeInterval: seconds, since: self) 
    } 

} 


// Try it 
let utcDate = Date().toGlobalTime() 
let localDate = utcDate.toLocalTime() 

print("utcDate - (utcDate)") 
print("localDate - (localDate)") 
Powiązane problemy