Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/Reference/Reference.html" rel="noreferrer"><code>NSDateComponents</code></a> you can extract day/month/year components from the date representing today, ignoring the hour/minutes/seconds components, add one day, and rebuild a date corresponding to tomorrow.</p> <p>So imagine you want to add exactly one day to the current date (including keeping hours/minutes/seconds information the same as the "now" date), you could add a timeInterval of 24*60*60 seconds to "now" using <code>dateWithTimeIntervalSinceNow</code>, but it is better (and DST-proof etc) to do it this way using <code>NSDateComponents</code>:</p> <pre><code>NSDateComponents* deltaComps = [[[NSDateComponents alloc] init] autorelease]; [deltaComps setDay:1]; NSDate* tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:deltaComps toDate:[NSDate date] options:0]; </code></pre> <p>But <strong>if you want to generate the date corresponding to tomorrow at midnight</strong>, you could instead just retrieve the month/day/year components of the date representing now, <strong>without hours/min/secs part</strong>, and add 1 day, then rebuild a date:</p> <pre><code>// Decompose the date corresponding to "now" into Year+Month+Day components NSUInteger units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSDateComponents *comps = [[NSCalendar currentCalendar] components:units fromDate:[NSDate date]]; // Add one day comps.day = comps.day + 1; // no worries: even if it is the end of the month it will wrap to the next month, see doc // Recompose a new date, without any time information (so this will be at midnight) NSDate *tomorrowMidnight = [[NSCalendar currentCalendar] dateFromComponents:comps]; </code></pre> <p>P.S.: You can read really useful advice and stuff about date concepts in the <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039i" rel="noreferrer">Date and Time Programming Guide</a>, especially <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendars.html#//apple_ref/doc/uid/TP40003470" rel="noreferrer">here about date components</a>.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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