2012-07-26 12 views

Respuesta

23

Esto debería hacer lo que tiene:

NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; 
fmt.dateFormat = @"LLL d, yyyy - HH:mm:ss zzz"; 
NSDate *utc = [fmt dateFromString:@"June 14, 2012 - 01:00:00 UTC"]; 
fmt.timeZone = [NSTimeZone systemTimeZone]; 
NSString *local = [fmt stringFromDate:utc]; 
NSLog(@"%@", local); 

Tenga en cuenta que su ejemplo es incorrecto: cuando es 1 a.m. del 14 de junio en UTC, sigue siendo el 13 de junio en EST, 8 p.m. andard o las 9 PM horario de verano. En mi sistema programa imprime esta

Jun 13, 2012 - 21:00:00 EDT 
2

Esta convertido del GMT a la hora local, puede modificar un poco de tiempo UTC

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; 

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
[dateFormatter setTimeZone:gmt]; 
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; 
[dateFormatter release]; 

Tomado de iPhone: NSDate convert GMT to local time

4
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"MMMM d, yyyy - HH:mm:ss zzz"; // format might need to be modified 

NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; 
[dateFormatter setTimeZone:destinationTimeZone]; 

NSDate *oldTime = [dateFormatter dateFromString:utcDateString]; 

NSString *estDateString = [dateFormatter stringFromDate:oldTime]; 
2

Swift 3

var dateformat = DateFormatter() 
dateformat.dateFormat = "LLL d, yyyy - HH:mm:ss zzz" 
var utc: Date? = dateformat.date(fromString: "June 14, 2012 - 01:00:00 UTC") 
dateformat.timeZone = TimeZone.current 
var local: String = dateformat.string(from: utc) 
print(local) 


Swift 4: Fecha de Extensión UTC o GMT ⟺ local

//UTC or GMT ⟺ Local 

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) 
    } 

} 
Cuestiones relacionadas