Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective C: Converting string to nsdate issue
    primarykey
    data
    text
    <p>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?</p> <pre><code>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); </code></pre> <p>OUTPUT:</p> <pre><code>2012-02-03 10:50:50.480 UniversalApp[13114:707] diff:2679916.480563 totaldays:31 </code></pre> <p>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"!</p> <pre><code>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); </code></pre> <p>OUTPUT:</p> <pre><code>date:03/02/2012 10:25:34 to date:03/25/2012 10:25:34 </code></pre> <p>[EDIT]</p> <p>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!</p> <p>Corrected Code:</p> <pre><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); </code></pre> <p>OUTPUT: (notice many outputs are Nan!!)</p> <p>2012-02-03 11:30:23.198 UniversalApp[13207:707] For Date:03/02/2012 10:25:34</p> <p>2012-02-03 11:30:23.204 UniversalApp[13207:707] seconds:3889.204166 totaldays:0</p> <p>2012-02-03 11:30:23.506 UniversalApp[13207:707] For Date:02/02/2012 16:56:05</p> <p>2012-02-03 11:30:23.512 UniversalApp[13207:707] seconds:nan totaldays:0</p> <p>2012-02-03 11:30:23.818 UniversalApp[13207:707] For Date:02/02/2012 14:34:05</p> <p>2012-02-03 11:30:23.827 UniversalApp[13207:707] seconds:nan totaldays:0</p> <p>2012-02-03 11:31:25.253 UniversalApp[13207:707] ToDate: 02/02/2012 12:02:55</p> <p>2012-02-03 11:31:25.255 UniversalApp[13207:707] seconds:127710.255748 totaldays:1</p> <p>2012-02-03 11:32:06.424 UniversalApp[13207:707] For Date:01/02/2012 11:01:20</p> <p>2012-02-03 11:32:06.427 UniversalApp[13207:707] seconds:174646.427676 totaldays:2</p> <p>2012-02-03 11:32:06.639 UniversalApp[13207:707] For Date:31/01/2012 17:38:17</p> <p>2012-02-03 11:32:06.643 UniversalApp[13207:707] seconds:nan totaldays:0</p> <p>[EDIT]</p> <p>Replacing 'hh' with 'HH' resolved above issue!</p> <p>[EDIT]</p> <p>Another issue I ran into...phew!!</p> <p>This doesn't work if set 24 hours clock off! Based on <a href="https://stackoverflow.com/questions/2135267/nsdateformatter-with-24-hour-times">this</a>, it overrides by the user's settings.</p> <p>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.</p> <pre><code>// Check for the 'nan', return if seconds='nan' invalid date format. if(seconds != seconds) return nil; </code></pre> <p>// Proceed to do your work</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload