加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 如何将NSString转换为NSDate?

发布时间:2020-12-14 19:36:12 所属栏目:百科 来源:网络整理
导读:我一直绞尽脑汁没有运气.有人可以告诉我如何转换这个字符串: “2011-01-13T17:00:00 11:00” 进入NSDate? 解决方法 unicode日期格式doc是 here 另外,根据您的情况,您可以尝试这样做: // original stringNSString *str = [NSString stringWithFormat:@"2
我一直绞尽脑汁没有运气.有人可以告诉我如何转换这个字符串:

“2011-01-13T17:00:00 11:00”

进入NSDate?

解决方法

unicode日期格式doc是 here

另外,根据您的情况,您可以尝试这样做:

// original string
NSString *str = [NSString stringWithFormat:@"2011-01-13T17:00:00+11:00"];

// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// ignore +11 and use timezone name instead of seconds from gmt
[dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'+11:00'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
NSDate *dte = [dateFormat dateFromString:str];
NSLog(@"Date: %@",dte);

// back to string
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssZZZ"];
[dateFormat2 setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
NSString *dateString = [dateFormat2 stringFromDate:dte];
NSLog(@"DateString: %@",dateString);

[dateFormat release];
    [dateFormat2 release];

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读