Objective C:将字符串转换为 nsdate 问题
我花了几个小时来解决这个奇怪的问题。我的日期格式为“dd/mm/yyyy hh:mm:ss”,并且想知道我的日期和现在之间的秒数差异。我按照下面的方法做,但不明白为什么 31 天会出错?应该只有几个小时的差别!谁能帮我看看我在这里做错了什么吗?
NSString* datetime = @"03/02/2012 10:25:34"; // Today's date
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/mm/yyyy hh:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:datetime];
NSTimeInterval diffSeconds = [[NSDate date] timeIntervalSinceDate:dateFromString];
int totalDays = diffSeconds/(24*60*60);
NSLog(@"seconds:%f totaldays:%d",diffSeconds, totalDays);
输出:
2012-02-03 10:50:50.480 UniversalApp[13114:707] diff:2679916.480563 totaldays:31
然后我尝试再次将 dateFromString 转换为 NSDate,令我惊讶的是它打印了完全随机的日期。不确定以下如何可能?它应该给出我的原始日期“日期时间”!
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"dd/mm/yyyy hh:mm:ss"];
NSString* str = [formatter stringFromDate:dateFromString];
NSLog(@"date:%@ to date:%@", datetime, str);
输出:
date:03/02/2012 10:25:34 to date:03/25/2012 10:25:34
[编辑]
将“dd/mm/yyyy hh:mm:ss”替换为“dd/MM/yyyy hh:mm:ss”有效,但秒差仍然不正确。请参阅以下不同日期的输出!
更正后的代码:
NSLog(@"For Date:%@",[res objectForKey:@"CommentDate"]);
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:[res objectForKey:@"CommentDate"]];
NSTimeInterval diffSeconds = [[NSDate date] timeIntervalSinceDate:dateFromString];
int totalDays = diffSeconds/(24*60*60);
NSLog(@"seconds:%f totaldays:%d",diffSeconds, totalDays);
输出:(注意许多输出都是 Nan!)
2012-02-03 11:30:23.198 UniversalApp[13207:707] 日期:03/02/2012 10:25:34
2012-02-03 11: 30:23.204 通用应用程序[13207:707]秒:3889.204166 总天数:0
2012-02-03 11:30:23.506 UniversalApp[13207:707] 日期:02/02/2012 16:56:05
2012-02-03 11:30:23.512 UniversalApp[13207:707 ] 秒:nan总天数:0
2012-02-03 11:30:23.818 UniversalApp[13207:707] 日期:02/02/2012 14:34:05
2012-02-03 11:30:23.827 UniversalApp[13207:707] 秒:楠 总天数:0
2012-02-03 11:31:25.253 UniversalApp[13207:707] 迄今为止:02/02/2012 12:02:55
2012-02-03 11:31:25.255 UniversalApp[13207:707] 秒:127710.255748 总天数:1
2012-02-03 11:32:06.424 UniversalApp[13207:707] 日期:01/02/2012 11:01:20
2012-02-03 11:32:06.427 UniversalApp[13207:707] 秒:174646.427676 总天数: 2
2012-02-03 11:32:06.639 UniversalApp[13207:707] 日期:31/01/2012 17:38:17
2012-02-03 11:32:06.643 UniversalApp[13207:707] 秒:nan 总天数: 0
[编辑]
将 'hh' 替换为“HH”解决了上述问题!
[编辑]
我遇到的另一个问题......唷!!
如果设置 24 小时时钟,则此功能不起作用!基于此,它会被用户的设置覆盖。
对于最后一个问题,我找不到任何强有力的解决方案。最后,我决定检查“nan”值的秒数,如果是“nan”,则使用原始日期字符串,不要转换为秒、小时等。
// Check for the 'nan', return if seconds='nan' invalid date format.
if(seconds != seconds)
return nil;
// 继续做你的工作
I spent couple of hours to resolve this weird issue. I have date formate in "dd/mm/yyyy hh:mm:ss" and want to know the difference in seconds between my date and NOW. I am doing it like below but not getting why it is getting 31 days wrong? It should be just few hours difference! Could anyone please help me what am I doing wrong here?
NSString* datetime = @"03/02/2012 10:25:34"; // Today's date
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/mm/yyyy hh:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:datetime];
NSTimeInterval diffSeconds = [[NSDate date] timeIntervalSinceDate:dateFromString];
int totalDays = diffSeconds/(24*60*60);
NSLog(@"seconds:%f totaldays:%d",diffSeconds, totalDays);
OUTPUT:
2012-02-03 10:50:50.480 UniversalApp[13114:707] diff:2679916.480563 totaldays:31
Then I tried to convert dateFromString to NSDate again and I surprised it printed totally random date. Not sure how is following possible? It should give my original date "datetime"!
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"dd/mm/yyyy hh:mm:ss"];
NSString* str = [formatter stringFromDate:dateFromString];
NSLog(@"date:%@ to date:%@", datetime, str);
OUTPUT:
date:03/02/2012 10:25:34 to date:03/25/2012 10:25:34
[EDIT]
Replacing "dd/mm/yyyy hh:mm:ss" to "dd/MM/yyyy hh:mm:ss" worked but still seconds difference is not coming correct. See following outputs for different dates!
Corrected Code:
NSLog(@"For Date:%@",[res objectForKey:@"CommentDate"]);
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:[res objectForKey:@"CommentDate"]];
NSTimeInterval diffSeconds = [[NSDate date] timeIntervalSinceDate:dateFromString];
int totalDays = diffSeconds/(24*60*60);
NSLog(@"seconds:%f totaldays:%d",diffSeconds, totalDays);
OUTPUT: (notice many outputs are Nan!!)
2012-02-03 11:30:23.198 UniversalApp[13207:707] For Date:03/02/2012 10:25:34
2012-02-03 11:30:23.204 UniversalApp[13207:707] seconds:3889.204166 totaldays:0
2012-02-03 11:30:23.506 UniversalApp[13207:707] For Date:02/02/2012 16:56:05
2012-02-03 11:30:23.512 UniversalApp[13207:707] seconds:nan totaldays:0
2012-02-03 11:30:23.818 UniversalApp[13207:707] For Date:02/02/2012 14:34:05
2012-02-03 11:30:23.827 UniversalApp[13207:707] seconds:nan totaldays:0
2012-02-03 11:31:25.253 UniversalApp[13207:707] ToDate: 02/02/2012 12:02:55
2012-02-03 11:31:25.255 UniversalApp[13207:707] seconds:127710.255748 totaldays:1
2012-02-03 11:32:06.424 UniversalApp[13207:707] For Date:01/02/2012 11:01:20
2012-02-03 11:32:06.427 UniversalApp[13207:707] seconds:174646.427676 totaldays:2
2012-02-03 11:32:06.639 UniversalApp[13207:707] For Date:31/01/2012 17:38:17
2012-02-03 11:32:06.643 UniversalApp[13207:707] seconds:nan totaldays:0
[EDIT]
Replacing 'hh' with 'HH' resolved above issue!
[EDIT]
Another issue I ran into...phew!!
This doesn't work if set 24 hours clock off! Based on this, it overrides by the user's settings.
I couldn't find any strong solution for this last issue. Finally I decided to check for 'nan' value for seconds and if it is 'nan' then use original date string and don't convert into seconds, hours etc.
// Check for the 'nan', return if seconds='nan' invalid date format.
if(seconds != seconds)
return nil;
// Proceed to do your work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于月份,它应该是“MM”。
将以下行更改
为
编辑
抱歉,格式应该是
For month, it should be 'MM'.
Change the following line
to
Edit
Sorry, the format should be
检查您的格式字符串,您需要@“dd/MM/yyyy hh:mm:ss”。就你所拥有的而言,你将分钟视为月份。
Check your format string, you want @"dd/MM/yyyy hh:mm:ss". With what you have you are treating minutes as the month.
试试这个
Try This