目标C:将字符串转换为nsdate问题

时间:2012-02-03 11:01:52

标签: objective-c nsdate nsdateformatter nstimeinterval string-to-datetime

我花了几个小时来解决这个奇怪的问题。我有“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,我惊讶于它完全随机打印日期。不确定如何追随可能?它应该给我原来的日期“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);

输出:

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);

输出:(注意很多输出是南!!)

2012-02-03 11:30:23.198 UniversalApp [13207:707]日期:03/02/2012 10:25:34

2012-02-03 11:30:23.204 UniversalApp [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 totaldays: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]秒: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]秒: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 totaldays:0

[编辑]

用'HH'替换'hh'解决了上述问题!

[编辑]

我遇到的另一个问题...... p !! [/ p>

如果设置24小时关闭,这不起作用!基于this,它将覆盖用户的设置。

我找不到任何有力的解决方案。最后我决定检查'nan'值为秒,如果是'nan'则使用原始日期字符串,不要转换为秒,小时等。

// Check for the 'nan', return if seconds='nan' invalid date format.
if(seconds != seconds)
    return nil;

//继续做你的工作

3 个答案:

答案 0 :(得分:4)

一个月,它应该是'MM'。

更改以下行

[dateFormatter setDateFormat:@"dd/mm/yyyy hh:mm:ss"];

[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss"];

修改 对不起,格式应为

[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];

答案 1 :(得分:1)

检查你的格式字符串,你想要@“dd / MM / yyyy hh:mm:ss”。根据你所拥有的,你将分钟视为月份。

答案 2 :(得分:0)

试试这个

        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"dd-MM-yyyy HH:mm:ss"];

        NSDate *date = [df dateFromString:@"12-12-2013 15:50:50"];

         NSLog(@"Converted Date-->%@",date);

        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
        [dateComponents1 setDay:6];

        NSDate *afterSomeDays = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents1 toDate:date options:0];

        NSLog(@"afterfifteenDays: %@", afterSomeDays);