iPhone:Find the current timezone offset in HOURS

后端 未结 8 679
萌比男神i
萌比男神i 2021-02-05 03:11

How to find out the current timezone offset on the device in HOURS ?

相关标签:
8条回答
  • 2021-02-05 03:56
    NSDate *sourceDate = [NSDate dateWithTimeIntervalSinceNow:3600 * 24 * 60];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    float timeZoneOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate] / 3600.0;
    NSLog(@"sourceDate=%@ timeZoneOffset=%d", sourceDate, timeZoneOffset);
    
    0 讨论(0)
  • 2021-02-05 03:59

    Don't use an int!!! I copied this code and now have a bug in my app. Some timezones are .5 hrs

    use this:

    float offset = [timezone secondsFromGMTForDate:self.date] / 3600.0;
    

    and don't forget the .0 behind 3600

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