1

When i convert the time from my Xcode iPhone simulator my code working fine and the output as it showing here:

2014-02-12 18:11:52 +0000

Local Time Zone (Europe/London (GMT) offset 0)

0

but when i try using my app on my iPhone 5 the output changed to

1970-01-01 12:00:00 am +0000

Local Time Zone (Europe/London (GMT) offset 0)

0

i am using xcode 5.0 and iOS 7

-(NSDate *)convertGMTtoLocal:(NSString *)gmtDateStr {

    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSTimeZone *gmt = [NSTimeZone systemTimeZone];

    [formatter setTimeZone:gmt];

    NSDate *GMTTime = [formatter dateFromString:gmtDateStr];

    NSTimeZone *tz = [NSTimeZone localTimeZone];
    NSLog(@"%@",tz);
    NSInteger seconds = [tz secondsFromGMTForDate: GMTTime];
    NSLog(@"%ld",(long)seconds);
    NSLog(@"%@",[NSDate dateWithTimeInterval: seconds sinceDate: GMTTime]);
    return [NSDate dateWithTimeInterval: seconds sinceDate: GMTTime];

}

Thanks,

Krunal
  • 77,632
  • 48
  • 245
  • 261

2 Answers2

1

NSLog logs dates in GMT. Don't use that. Create a date formatter and use that to log your date. The other poster's code is exactly backwards, and will convert the date TO GMT. Leave off the setTimeZone call in vborra's code and it should give you the date in local time.

The code might look like this:

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateStyle:NSDateFormatterNoStyle];
  [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
  NSString *time = [dateFormatter stringFromDate:[NSDate date]];
  NSLog(@"Time is %@", time);
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • But the OP's problem is that he didn't set the locale. – Hot Licks Feb 12 '14 at 23:44
  • No, the OPs problem is that he logged the NSDate directly. That calls NSDate's description method, which displays the date in GMT. A date formatter will default to the user's current locale. If you need a different locale (either for a different time zone or a different time format) you CAN change it, but most likely the default locale will be the correct one. – Duncan C Feb 13 '14 at 00:20
  • Look at what he logged -- one shows AM and the other doesn't. A clear sign of the classical locale problem. Follow the link, if you don't believe it. – Hot Licks Feb 13 '14 at 00:44
  • Hot Licks solve my issue thank you guys, nothing wrong with the format, the idea of my app is receiving the time from server and convert it to the local time. now everything working fine. the problem with AM and PM – user3116050 Feb 13 '14 at 01:22
0

May this extension would be easier.

Swift 4: UTC/GMT ⟺ Local (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)")
Krunal
  • 77,632
  • 48
  • 245
  • 261