1

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

Community
  • 1
  • 1
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139

3 Answers3

4

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"];
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • OMG, can't believe I spent my morning on this! Thanks. – Paresh Masani Feb 03 '12 at 11:18
  • still there is some issues in calculating seconds. Please see edited question. Thanks. – Paresh Masani Feb 03 '12 at 11:34
  • It's may be I have used 24 hours time and it expecting 12 hours? How to specify to allow 24 hours? Thanks. – Paresh Masani Feb 03 '12 at 11:38
  • Yeah, even I too , just noticed – Ilanchezhian Feb 03 '12 at 11:48
  • still there is a problem...this doesn't work if set 24 hours clock off! Based on this - http://stackoverflow.com/questions/2135267/nsdateformatter-with-24-hour-times , it overrides by the user's settings..I will need to check first what is the user's setting. Will edit once I am done. Let me know if you have handy to check user's clock settings. Thanks. – Paresh Masani Feb 03 '12 at 14:07
  • Hi Aadhira: this issue seems to be little bit complicated! I have a date format in 24 hours coming from Server so no matter what user's local settings of iPhone I need to user 24 hours display to calculate the seconds. Currently it returns 'nan' when user has 24 hours clock off! Not sure how could I deal with this! – Paresh Masani Feb 03 '12 at 14:34
  • Posted my weak solution. I don't think there is a way to meet my requirements! – Paresh Masani Feb 03 '12 at 15:07
1

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

David Knight
  • 763
  • 6
  • 12
0

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);
Mritunjay
  • 167
  • 6