12

Input String: June 14, 2012 - 01:00:00 UTC

Output Local String: Jun 13, 2012 - 21:00:00 EDT

I like to get the offset from

NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSLog(@"Time Zone: %@", destinationTimeZone.abbreviation);

Any suggestion ?

Krunal
  • 77,632
  • 48
  • 245
  • 261
AAV
  • 3,785
  • 8
  • 32
  • 59

5 Answers5

23

This should do what you need:

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

Note that your example is incorrect: when it's 1 AM on June-14th in UTC, it's still June-13th in EST, 8 PM standard or 9 PM daylight savings time. On my system this program prints

Jun 13, 2012 - 21:00:00 EDT
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
6

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: Date Extension UTC or 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)
    }

}
Krunal
  • 77,632
  • 48
  • 245
  • 261
3
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];
Dima
  • 23,484
  • 6
  • 56
  • 83
1

This convert from GMT to Local Time, you can modify it a bit for UTC Time

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];

Taken from iPhone: NSDate convert GMT to local time

Community
  • 1
  • 1
feco
  • 4,384
  • 5
  • 27
  • 44
0
func timeConverter() -> 
{
    let zone = TimeZone.current
    let sec = -TimeInterval(zone.secondsFromGMT(for: self)
    return Date(timeInterval: sec , since : self)
}
Meer
  • 2,765
  • 2
  • 19
  • 28
Haya Hashmat
  • 137
  • 10