Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Because this is apparently my most popular answer, I'll try to edit it to contain a little bit more information.</p> <p>Despite its name, <code>NSDate</code> in and of itself simply marks a point in machine time, not a date. There's no correlation between the point in time specified by an <code>NSDate</code> and a year, month, or day. For that, you have to refer to a calendar. Any given point in time will return different date information based on what calendar you're looking at (dates are not the same in both the Gregorian and Jewish calendars, for instance), and while the Gregorian calendar is the most widely used calendar in the world - I'm assuming - we're a little biased that <code>NSDate</code> should always use it. <code>NSDate</code>, luckily, is far more bipartisan.</p> <hr> <p>Getting date and time is going to have to pass through <code>NSCalendar</code>, as you mentioned, but there's a simpler way to do it:</p> <pre><code>NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; </code></pre> <p>That generates an <code>NSDateComponents</code> object containing the day, month, and year from the current system calendar for the current day. (<strong>Note:</strong> this isn't necessarily the current <em>user-specified</em> calendar, just the default system one.)</p> <p>Of course, if you're using a different calendar or date, you can easily change that. A list of available calendars and calendar units can be found in the <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html#//apple_ref/c/tdef/NSCalendarUnit" rel="noreferrer"><code>NSCalendar</code> Class Reference</a>. More information about <code>NSDateComponents</code> can be found in the <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/Reference/Reference.html" rel="noreferrer"><code>NSDateComponents</code> Class Reference</a>.</p> <hr> <p>For reference, accessing individual components from the <code>NSDateComponents</code> is rather easy:</p> <pre><code>NSInteger day = [components day]; NSInteger month = [components month]; NSInteger year = [components year]; </code></pre> <p>You just have to be mindful: <code>NSDateComponents</code> won't contain valid information for any fields you ask for unless you generated them with that valid information (i.e. request <code>NSCalendar</code> to provide that information with <code>NSCalendarUnit</code>s). <code>NSDateComponents</code> contain no reference information in and of themselves - they're just simple structures that hold numbers for you to access. If you want to also get an era, for instance, out of <code>NSDateComponents</code>, you'll have to feed the generator method from <code>NSCalendar</code> with the <code>NSCalendarUnitEra</code> flag.</p>
 

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