gpt4 book ai didi

iphone - 字符串格式的 Nsdate 不起作用

转载 作者:行者123 更新时间:2023-11-29 11:16:54 24 4
gpt4 key购买 nike

我正在尝试将字符串日期转换为 NSDate 以对格式进行一些更改......但它始终返回 nil!

这是我的代码:

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

//Mon Jan 30 17:40:12 +0000 2012
[df setDateFormat:@"ccc MMM dd HH:mm:ss Z yyyy"];
[df setTimeStyle:NSDateFormatterFullStyle];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];

NSDate *date1 = [df dateFromString:strData];

我也试过这种格式:

[df setDateFormat:@"EEE MMM dd HH:mm:ss z yyyy"];

我试过这张表:

http://www.deanoj.co.uk/ios-development/nsdateformatter-formatting-strings-reference/

我试过这些链接……还有很多其他链接:

Convert NSString to NSDate with string format "Sun, 08 Jan 2012 13:57:38 +0000"

NSDateFormatter dateFromString returns nil

How to format Facebook/Twitter dates (from the JSON feed) in Objective-C

有人可以帮我吗?

提前致谢!

最佳答案

如果我把你的代码修改成这样:

[df setDateFormat:@"ccc MMM dd HH:mm:ss Z yyyy"];
// [df setTimeStyle:NSDateFormatterFullStyle];
// [df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];

NSDate *date1 = [df dateFromString:@"Mon Jan 30 17:40:12 +0000 2012"];
NSLog( @"date is %@", date1);

我得到:

2012-01-30 11:36:38.339 TestApp[32182:f803] date is 2012-01-30 17:40:12 +0000

似乎您需要做的就是注释(或删除)这两行。

关于iphone - 字符串格式的 Nsdate 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9067216/

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