Objective C: Converting string to nsdate issue

4.2k views Asked by At

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

3

There are 3 answers

7
Ilanchezhian On BEST ANSWER

For month, it should be 'MM'.

Change the following line

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

to

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

Edit Sorry, the format should be

[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
0
David Knight On

Check your format string, you want @"dd/MM/yyyy hh:mm:ss". With what you have you are treating minutes as the month.

0
Mritunjay On

Try This

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