【问题标题】:Get system time zone from NSTimeZone in "GMT 5:30" NSString format从“GMT 5:30”NSString 格式的 NSTimeZone 获取系统时区
【发布时间】:2015-10-14 05:53:07
【问题描述】:

我对目标 C 有点陌生。 我正在尝试以字符串格式获取系统时区。我的字符串中只需要“GMT 5:30”。

_devTimezone = (NSString*)[NSTimeZone systemTimeZone];

【问题讨论】:

    标签: objective-c nsstring nstimezone


    【解决方案1】:

    这样做。

    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    
    //set timezone to GMT 5:30.
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    
    NSString *currentTime = [dateFormatter stringFromDate:currentDate];
    NSLog(@"User's current time %@",currentTime);
    

    希望对你有所帮助。

    【讨论】:

    • 它给出了当前时间.. 不是所需的输出格式 "GMT:5:30"... 无论如何! :)
    • 查看更新的答案。它以字符串格式提供带有 GMT 5:30 时区的日期。
    【解决方案2】:

    您可以使用NSTimeZone 方法secondsFromGMT 获取偏移量作为NSInteger 值,然后使用NSString 方法stringWithFormat 和基本算术将整数值划分为小时和分钟。

    示例代码:

    - (NSString *) getTimeZoneOffset:(NSTimeZone *)timezone
    {
       NSInteger minutesFromGMT = timezone.secondsFromGMT / 60; 
    
       // handle negative offsets
       char sign;
       if (minutesFromGMT < 0)
       {
          sign = '-';
          minutesFromGMT = -minutesFromGMT; // make positive
       }
       else
          sign = '+';
    
       // split in hours and minutes
       NSInteger hoursFromGMT = minutesFromGMT / 60;
       minutesFromGMT %= 60;
    
       // format as string - making sure minutes is two digits with leading zero if needed
       // cast to int is OK as no value will be greater than 59!
       return [NSString stringWithFormat:@"GMT%c%d:%02d", sign, (int)hoursFromGMT, (int)minutesFromGMT];
    }
    

    HTH

    【讨论】:

      【解决方案3】:

      您可以将日期描述作为字符串获取,然后将所需的子字符串获取为

      NSString *myDate = [[NSDate date] descriptionWithLocale:[NSLocale systemLocale]];
      myDate=[myDate substringWithRange:NSMakeRange(26, 9)];
      NSLog(@"%@",myDate);
      

      你会在日志中得到 GMT+05:30。

      ****已编辑****

      如果你不确定什么是索引 您可以设置日期格式,然后访问索引

      NSString *dateStr = @"Wed, 14 Oct 2015 12:53:58 +0000";
      
       // Convert string to date object
      NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
      [dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"];
      NSDate *date = [dateFormat dateFromString:dateStr];
      NSString *myDate = [date descriptionWithLocale:[NSLocale systemLocale]];
      myDate=[myDate substringWithRange:NSMakeRange(26, 9)]; 
      NSLog(@"%@",myDate);
      

      编码愉快.. :)

      【讨论】:

      • 肯定descriptionWithLocale 并不总是返回时区偏移量为 26 的字符串?
      • 我已经按照问题回答了。你能告诉我在哪种情况下时区不是偏移 26。我肯定会找到解决方案。
      • 在访问索引之前,我已将其格式设置为如上。这将确保起始索引为 26。现在我希望它能正常工作。
      • 不客气 chetan.. 如果它对您有用,请点赞答案.. 谢谢..
      猜你喜欢
      • 2013-02-12
      • 2016-12-14
      • 2011-12-28
      • 2015-07-21
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 2013-02-03
      相关资源
      最近更新 更多