title: iOS and NSDate functions for last 7 days
body: <p>I have the following code to generate the current date in order to allow me to show the number of steps taken today.</p>
  
  <p>However I would like to display the previous 7 days data that is stored, but am unsure how to make the date query show each of the last 7 days so that I can then make the 7 queries. I am thinking it is possible to write the code below 7 times, with a -1 -2 -3 etc. but am unsure where I would do the adjustment to get prior dates.</p>
  
  <p>Could someone shed some light onto it for me?</p>
  
  <pre><code>// Calculate start of day
  
  - (NSDate * )startDateOfToday
  {
      NSCalendar *calendar = [NSCalendar currentCalendar];
      NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
                                                 fromDate:[NSDate date]];
  
      return [calendar dateFromComponents:components];
  }
  </code></pre>
  
  <p>Thanks.</p>