将 ISO 8601 时间戳转换为 NSDate:如何处理 UTC 时间偏移?

发布于 2024-10-20 14:09:47 字数 321 浏览 2 评论 0原文

我在将 ISO 8601 时间戳转换为 NSDate 时遇到问题。我尝试使用 NSDateFormatter,但无法使其与出现在时间戳末尾的 UTC 时间偏移一起使用。为了解释一下,我想将如下所示的时间戳转换为 NSDate2011-03-03T06:00:00-06:00。我的问题是:如何处理“-06:00”部分?我尝试使用 yyyy-MM-dd'T'HH:mm:ssZ 作为我的日期格式字符串,但它不起作用。有什么建议吗?

I'm having trouble converting an ISO 8601 timestamp into an NSDate. I tried to use NSDateFormatter, but I can't get it to work with the UTC time offset that appears on the end of the timestamps. To explain, I would like to convert a timestamp such as the following into an NSDate: 2011-03-03T06:00:00-06:00. My question is: How do I deal with the "-06:00" part? I tried using yyyy-MM-dd'T'HH:mm:ssZ as my date format string but it doesn't work. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情魔剑神 2024-10-27 14:09:47

无需删除 : 。要处理“00:00”样式时区,您只需要“ZZZZ”:

Swift

let dateString = "2014-07-06T07:59:00Z"

let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
dateFormatter.dateFromString(dateString)

Objective-C

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSString *input = @"2013-05-08T19:03:53+00:00";
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"]; //iso 8601 format
NSDate *output = [dateFormat dateFromString:input];
NSLog(@"Date output: %@", output);

No need to remove the :'s. To handle the "00:00" style timezone, you just need "ZZZZ":

Swift

let dateString = "2014-07-06T07:59:00Z"

let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
dateFormatter.dateFromString(dateString)

Objective-C

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSString *input = @"2013-05-08T19:03:53+00:00";
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"]; //iso 8601 format
NSDate *output = [dateFormat dateFromString:input];
NSLog(@"Date output: %@", output);
只有影子陪我不离不弃 2024-10-27 14:09:47

就我而言,我收到了类似的内容:

“2015-05-07T16:16:47.054403Z”

并且我必须使用:

“yyyy'-'MM'-'dd'T'HH':'mm':'ss。 SSZ"

In my case I received something like that:

"2015-05-07T16:16:47.054403Z"

And I had to use:

"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZ"

影子的影子 2024-10-27 14:09:47

问题是时区偏移内的 : 字符。您可以显式删除该冒号,或删除所有冒号,然后继续。例如:

NSString *s = @"2011-03-03T06:00:00-06:00";
s = [s stringByReplacingOccurrencesOfString:@":" withString:@""];
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd'T'HHmmssZZ"];
NSDate *d = [f dateFromString:s];
NSLog(@"%@", d);

此日志:

EmptyFoundation[5088:707] 2011-03-03 12:00:00 +0000

The problem is the : character inside the timezone offset. You could explicitly remove just that colon, or remove all of the colons, and then proceed. For example:

NSString *s = @"2011-03-03T06:00:00-06:00";
s = [s stringByReplacingOccurrencesOfString:@":" withString:@""];
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd'T'HHmmssZZ"];
NSDate *d = [f dateFromString:s];
NSLog(@"%@", d);

This logs:

EmptyFoundation[5088:707] 2011-03-03 12:00:00 +0000
醉城メ夜风 2024-10-27 14:09:47

这是我使用的方法:

-(NSDate *)dateFromISO8601String:(NSString *)dateString{
    if (!dateString) return nil;
    if ([dateString hasSuffix:@"Z"]) {
      dateString = [[dateString substringToIndex:(dateString.length-1)] stringByAppendingString:@"-0000"];
    }
    dateString = [dateString stringByReplacingOccurrencesOfString:@":" withString:@""];
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HHmmssZ";
    return [dateFormatter dateFromString:dateString];
}

Here is the method that I use:

-(NSDate *)dateFromISO8601String:(NSString *)dateString{
    if (!dateString) return nil;
    if ([dateString hasSuffix:@"Z"]) {
      dateString = [[dateString substringToIndex:(dateString.length-1)] stringByAppendingString:@"-0000"];
    }
    dateString = [dateString stringByReplacingOccurrencesOfString:@":" withString:@""];
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HHmmssZ";
    return [dateFormatter dateFromString:dateString];
}
草莓味的萝莉 2024-10-27 14:09:47

在 Swift 中将 ISO 8601 时间戳转换为 NSDate:

let dateFormatter = NSDateFormatter()
let inputDate = "2015-06-18T19:00:53-07:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ" //iso 8601

let outputDate = dateFormatter.dateFromString(inputDate)
println(outputDate!) //optional implicitly

或者显式可选:

if let outputDate = dateFormatter.dateFromString(inputDate) {
     println(outputDate) //no need !
}

Converting ISO 8601 timestamp into NSDate In Swift:

let dateFormatter = NSDateFormatter()
let inputDate = "2015-06-18T19:00:53-07:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ" //iso 8601

let outputDate = dateFormatter.dateFromString(inputDate)
println(outputDate!) //optional implicitly

Or optional explicitly:

if let outputDate = dateFormatter.dateFromString(inputDate) {
     println(outputDate) //no need !
}
摇划花蜜的午后 2024-10-27 14:09:47

Apple 支持 ISO 8601 格式的单独格式

Objective C

NSISO8601DateFormatter *formater = [[NSISO8601DateFormatter alloc]init];
NSString *string = [formater stringFromDate:[NSDate date]];

Apple supports a separate format for ISO 8601 format

Objective C

NSISO8601DateFormatter *formater = [[NSISO8601DateFormatter alloc]init];
NSString *string = [formater stringFromDate:[NSDate date]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文