gpt4 book ai didi

ios - 如何在 iOS 8.1 中转换 "Tue, 14 Jul 2015 15:00:00 +0000"

转载 作者:行者123 更新时间:2023-11-29 01:51:00 26 4
gpt4 key购买 nike

我尝试通过这种方式将字符串转换为 NSDate:

NSString *str = @"Tue, 14 Jul 2015 15:00:00 +0000";

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss zzzz"];

NSDate* aDate = [formatter dateFromString:str];

[formatter setDateFormat:@"MMM dd,yyyy HH:mm:ss"];

NSLog(@"%@", [formatter stringFromDate:aDate]);

但是它是null,什么是真正的方式?

最佳答案

我试过了,应该可以,结果是:

Jul 14,2015 16:00:00 (based on my time zone)

代码如下:

NSString *str = @"Tue, 14 Jul 2015 15:00:00 +0000";

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];


[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];

NSDate* aDate = [formatter dateFromString:str];

[formatter setDateFormat:@"MMM dd,yyyy HH:mm:ss"];
//formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; if you want to avoid converting the time to the current timezone

NSLog(@"%@", [formatter stringFromDate:aDate]);

Date Format Patterns

关于ios - 如何在 iOS 8.1 中转换 "Tue, 14 Jul 2015 15:00:00 +0000",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31420266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com