【问题标题】:How to convert UTC date string to local time (systemTimeZone)如何将 UTC 日期字符串转换为本地时间 (systemTimeZone)
【发布时间】:2012-07-25 12:36:00
【问题描述】:

输入字符串:2012 年 6 月 14 日 - 01:00:00 UTC

输出本地字符串:2012 年 6 月 13 日 - 21:00:00 EDT

我喜欢从

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

有什么建议吗?

【问题讨论】:

    标签: ios objective-c swift date timezone


    【解决方案1】:

    这应该可以满足您的需要:

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

    请注意,您的示例不正确:当 UTC 时间为 6 月 14 日凌晨 1 点时,仍然是美国东部标准时间 6 月 13 日、标准时间晚上 8 点或夏令时晚上 9 点。在我的系统上这个程序打印

    Jun 13, 2012 - 21:00:00 EDT
    

    【讨论】:

      【解决方案2】:

      斯威夫特 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:日期扩展 UTC 或 GMT ⟺ 本地

      //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)
          }
      
      }
      

      【讨论】:

        【解决方案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];
        

        【讨论】:

          【解决方案4】:

          这会从 GMT 转换为本地时间,您可以将其修改为 UTC 时间

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

          取自iPhone: NSDate convert GMT to local time

          【讨论】:

            【解决方案5】:
            func timeConverter() -> 
            {
                let zone = TimeZone.current
                let sec = -TimeInterval(zone.secondsFromGMT(for: self)
                return Date(timeInterval: sec , since : self)
            }
            

            【讨论】:

              猜你喜欢
              • 2018-02-23
              • 2011-06-13
              • 2014-02-22
              • 1970-01-01
              • 2012-08-04
              • 2023-03-14
              • 2015-10-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多