NSDateFormatter dateFromString 返回 null

发布于 2025-01-02 15:15:37 字数 353 浏览 4 评论 0原文

我正在尝试将字符串日期放入日期对象中,以便我可以进行一些比较。我的代码如下:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy/MM/dd_hh:mm:ss"];
NSDate *date = [formatter dateFromString:@"2012/02/06_20:35:59"]; 
[formatter release];

NSLog(@"date = %@", date);

我的 NSLog 显示 *date = null;

任何帮助表示赞赏。谢谢。

I am trying to get a string date into a date object so I can do some comparisons. My code is as follows:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy/MM/dd_hh:mm:ss"];
NSDate *date = [formatter dateFromString:@"2012/02/06_20:35:59"]; 
[formatter release];

NSLog(@"date = %@", date);

My NSLog is showing that *date = null;

Any help is appreciated. Thanks.

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

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

发布评论

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

评论(2

你在看孤独的风景 2025-01-09 15:15:37

您的格式不需要是 yyyy/MM/dd_HH:mm:ss 吗?

hh 为 0-12,而 HH 为 0-23。

作为参考,这里是我从 - http:// /unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

Does't your format need to be yyyy/MM/dd_HH:mm:ss?

hh is 0-12 where as HH is 0-23.

For reference, here is the doc where I got that nugget of information from - http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

顾冷 2025-01-09 15:15:37
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formatter dateFromString:@"2012-02-06 20:35:59"]; 
    [formatter release];

    NSLog(@"date = %@", date);


d Day of the month as digits; no leading zero for single-digit days.
dd Day of the month as digits; leading zero for single-digit days.
ddd Day of the week as a three-letter abbreviation.
dddd Day of the week as its full name.
m Month as digits; no leading zero for single-digit months.
mm Month as digits; leading zero for single-digit months.
mmm Month as a three-letter abbreviation.
mmmm Month as its full name.
yy Year as last two digits; leading zero for years less than 10.
yyyy Year represented by four digits.
h Hours; no leading zero for single-digit hours (12-hour clock).
hh Hours; leading zero for single-digit hours (12-hour clock).
H Hours; no leading zero for single-digit hours (24-hour clock).
HH Hours; leading zero for single-digit hours (24-hour clock).
M Minutes; no leading zero for single-digit minutes.
Uppercase M unlike CF timeFormat‘s m to avoid conflict with months.
MM Minutes; leading zero for single-digit minutes.
Uppercase MM unlike CF timeFormat‘s mm to avoid conflict with months.
s Seconds; no leading zero for single-digit seconds.
ss Seconds; leading zero for single-digit seconds.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formatter dateFromString:@"2012-02-06 20:35:59"]; 
    [formatter release];

    NSLog(@"date = %@", date);


d Day of the month as digits; no leading zero for single-digit days.
dd Day of the month as digits; leading zero for single-digit days.
ddd Day of the week as a three-letter abbreviation.
dddd Day of the week as its full name.
m Month as digits; no leading zero for single-digit months.
mm Month as digits; leading zero for single-digit months.
mmm Month as a three-letter abbreviation.
mmmm Month as its full name.
yy Year as last two digits; leading zero for years less than 10.
yyyy Year represented by four digits.
h Hours; no leading zero for single-digit hours (12-hour clock).
hh Hours; leading zero for single-digit hours (12-hour clock).
H Hours; no leading zero for single-digit hours (24-hour clock).
HH Hours; leading zero for single-digit hours (24-hour clock).
M Minutes; no leading zero for single-digit minutes.
Uppercase M unlike CF timeFormat‘s m to avoid conflict with months.
MM Minutes; leading zero for single-digit minutes.
Uppercase MM unlike CF timeFormat‘s mm to avoid conflict with months.
s Seconds; no leading zero for single-digit seconds.
ss Seconds; leading zero for single-digit seconds.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文