将 NSString 格式化为 NSDate

发布于 2024-12-20 05:14:02 字数 510 浏览 2 评论 0原文

我从 facebook 获取用户日期,看起来像

“生日日期” = “1987 年 3 月 4 日” 我写了这段代码

 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"dd/MM/YYYY"];
    NSDate *dateFromString = [dateFormatter dateFromString:birthDate];

    NSLog(@"dateformstring %@ ",dateFromString);
    [birthPicker setDate:dateFromString animated:YES];

和字符串 si 的结果日期:1988-12-25 23:00:00 +0000,我在选择器中选择了这个日期,我不知道这个日期在哪里:/

i take the user date from facebook , it look like

"birthday_date" = "03/04/1987"
i wrote this code

 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"dd/MM/YYYY"];
    NSDate *dateFromString = [dateFormatter dateFromString:birthDate];

    NSLog(@"dateformstring %@ ",dateFromString);
    [birthPicker setDate:dateFromString animated:YES];

and the result date from string si : 1988-12-25 23:00:00 +0000 , i have this date selected in the picker , I do not know where did this date :/

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

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

发布评论

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

评论(3

离线来电— 2024-12-27 05:14:02

尝试 [dateFormatter setDateFormat:@"dd/MM/yyyy"];

Try [dateFormatter setDateFormat:@"dd/MM/yyyy"];

沉默的熊 2024-12-27 05:14:02

哎呀。正如 tilo 所说,使用“yyyy”。正如 Apple 文档中所述:

“一个常见的错误是使用 YYYY。yyyy 指定日历年,而 YYYY 指定 ISO 中使用的年份(“一年中的周”)年周日历。”

oops. As tilo said, use "yyyy." As mentioned in the Apple docs:

"A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of "Week of Year"), used in the ISO year-week calendar."

失退 2024-12-27 05:14:02

为此制作了一个 NSString 扩展

// Simple as this.   
date = dateString.dateValue;

感谢 NSDataDetector,它识别了一大堆格式。

// Tested in GMT+1 (Hungary).
@"2014-01-16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014.01.16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014/01/16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014 Jan 16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014 Jan 16th" dateValue is <2014-01-16 11:00:00 +0000>
@"20140116" dateValue is <2014-01-16 11:00:00 +0000>
@"01-16-2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01.16.2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01/16/2014" dateValue is <2014-01-16 11:00:00 +0000>
@"16 January 2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01-16-2014 17:05:05" dateValue is <2014-01-16 16:05:05 +0000>
@"01-16-2014 T 17:05:05 UTC" dateValue is <2014-01-16 17:05:05 +0000>
@"17:05, 1 January 2014 (UTC)" dateValue is <2014-01-01 16:05:00 +0000>

eppz!kit 的一部分,获取类别 NSString+EPPZKit.h 来自 GitHub。

Made an NSString extension for that.

// Simple as this.   
date = dateString.dateValue;

Thanks to NSDataDetector, it recognizes a whole lot of format.

// Tested in GMT+1 (Hungary).
@"2014-01-16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014.01.16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014/01/16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014 Jan 16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014 Jan 16th" dateValue is <2014-01-16 11:00:00 +0000>
@"20140116" dateValue is <2014-01-16 11:00:00 +0000>
@"01-16-2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01.16.2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01/16/2014" dateValue is <2014-01-16 11:00:00 +0000>
@"16 January 2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01-16-2014 17:05:05" dateValue is <2014-01-16 16:05:05 +0000>
@"01-16-2014 T 17:05:05 UTC" dateValue is <2014-01-16 17:05:05 +0000>
@"17:05, 1 January 2014 (UTC)" dateValue is <2014-01-01 16:05:00 +0000>

Part of eppz!kit, grab the category NSString+EPPZKit.h from GitHub.

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