iPhone:Find the current timezone offset in HOURS

后端 未结 8 695
萌比男神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:38

    Use NSTimeZone's localTimeZone

    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    NSInteger seconds = [timeZone secondsFromGMT];
    NSLog(@"TZ : %@ : Seconds %ld",[timeZone abbreviation],(long)seconds);
    
    int h = (int)seconds / 3600;
    int m = (int)seconds / 60 % 60;
    
    NSString *strGMT = @"";
    if (h>=0)
        strGMT = [NSString stringWithFormat:@"+%02d:%02d", h, m];
    else
        strGMT = [NSString stringWithFormat:@"%03d:%02d", h, m];
    
    NSLog(@"GMT -/+ HH:mm: %@",strGMT);
    

提交回复
热议问题