Note that there are some explanatory texts on larger screens.

plurals
  1. PONSDate confusion dateFromString format
    text
    copied!<pre><code>//Grab current day from sys date all I am interested in is the day i.e 01 - 31 days of month NSDateFormatter *dayFormatter = [[NSDateFormatter alloc] init]; [dayFormatter setDateFormat:@"dd"]; NSString *dayString = [dayFormatter stringFromDate:[NSDate date]]; </code></pre> <p>Later on in my code i then want to convert this string dayString to NSDate like below but dayFromString however the format is not just day the log is 2000-01-16 00:00:00 +0000 all i want is the 16</p> <pre><code>NSDate *dayFromString = [dayFormatter dateFromString:dayString]; NSLog(@"Day from string %@", dayFromString); </code></pre> <p>EDIT:</p> <p>Maybe this explains why I need an NSDate object for dayFromString</p> <pre><code>//sepDates is a JSON array with comma separated dates i.e 13, 27, 29 of which I split it indexes so 13 is at index 0 27 at 1 and 29 at 2. May string is the string that holds the above dates sepDates = [mayString componentsSeparatedByString:@","]; //Day string is todays date i.e 16 (16th) NSDate *dayFromString = [dayFormatter dateFromString:dayString]; NSLog(@"Day from string %@", dayFromString); double min = [dayFromString timeIntervalSinceDate:[sepDates objectAtIndex:0]]; NSLog(@"Min %f", min); //I then want to calculate which of the dates in the sepDates array at index is closest to todays current day 16 int minIndex = 0; for (int d = 1; d &lt; [sepDates count]; ++d) { double currentmin = [dayFromString timeIntervalSinceDate:[sepDates objectAtIndex:d]]; if (currentmin &lt; min) { min = currentmin; minIndex = d; NSLog(@"minIndex = %d", minIndex); } } </code></pre>
 

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