Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here we go again...</p> <p>A date is an independent point in time. It exists and will always exist, regardless of whether or not humans and our time measurement devices exist. Time zones are an invention of man to help us not go crazy. The jury's still out on whether or not they're effective at that. :P</p> <p>When you talk about wanting a "date in EDT or any other timezone", you've failed to grasp what a date is. A timezone is simply one aspect of <em>how to display a point in time</em>.</p> <p>So, you have an <code>NSDate</code>. This date is the <em>correct</em> date, <em>regardless</em> of what timezone you're in. So how do you "display the time in the current time zone"? You use an <code>NSDateFormatter</code>.</p> <p>An <code>NSDate</code> is inherently unreadable. There are ways that we can <em>represent</em> a date, such as an interval of seconds (which is an arbitrarily long interval itself) from a different point in time. However, to make a date <em>human readable</em> can only be accomplished by formatting it into a string. So, let's do that:</p> <pre><code>NSDate *myDate = [NSDate date]; // or whatever date you want to make human-readable NSDateFormatter *f = [[NSDateFormatter alloc] init]; [f setDateStyle:NSDateFormatterLongStyle]; [f setTimeStyle:NSDateFormatterLongStyle]; NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames]; for (NSString *name in timeZoneNames) { NSTimeZone *tz = [NSTimeZone timeZoneWithName:name]; [f setTimeZone:tz]; NSLog(@"%@", [f stringFromDate:date]); } </code></pre> <p>Now, obviously the output for this is going to be enormously long (mine is over 400 lines long). But here's a sampling of that output:</p> <pre><code>... August 2, 2011 1:03:38 AM GMT-03:00 August 2, 2011 12:03:38 AM AST August 2, 2011 12:03:38 AM GMT-04:00 August 1, 2011 11:03:38 PM EST August 2, 2011 1:03:38 AM GMT-03:00 August 1, 2011 11:03:38 PM CDT August 2, 2011 12:03:38 AM AST August 2, 2011 1:03:38 AM GMT-03:00 August 1, 2011 10:03:38 PM CST August 2, 2011 12:03:38 AM AST August 2, 2011 12:03:38 AM GMT-04:00 August 1, 2011 11:03:38 PM GMT-05:00 August 1, 2011 10:03:38 PM MDT August 1, 2011 10:03:38 PM MDT August 2, 2011 12:03:38 AM GMT-04:00 August 1, 2011 11:03:38 PM CDT August 1, 2011 11:33:38 PM GMT-04:30 August 2, 2011 1:03:38 AM GMT-03:00 August 1, 2011 11:03:38 PM EST August 1, 2011 11:03:38 PM CDT August 1, 2011 10:03:38 PM MDT ... </code></pre> <p>So, what do we see here? The <em>same date</em> has a whole bunch of <em>different</em> string representations! NEAT! In other words, the timezone of the <em>formatter</em> is what's used in determining what the outputted string looks like.</p> <p>And so: the date itself has <strong>NOTHING</strong> to do with the timezone. If you want a date to <em>look</em> a certain way, you have to configure the <em>formatter</em> to make it look that way.</p> <hr> <p><strong>Update</strong> From your comment, I think I get what you want. You want the <em>date components</em> formatted for a certain timezone? In other words, you have a point in time, and you want to know what hour/minute/second that is in (say) the US Eastern timezone?</p> <p>If that's the case, then you do that based on the <em>calendar</em>:</p> <pre><code>NSDate *date = [NSDate date]; NSCalendar *cal = [NSCalendar currentCalendar]; [cal setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]]; NSDateComponents *components = [cal components:NSUIntegerMax fromDate:date]; NSLog(@"%@", components); </code></pre> <p>And this will give you:</p> <pre><code>&lt;NSDateComponents: 0x100124240&gt; Calendar: &lt;CFCalendar 0x100133e90 [0x7fff7605cea0]&gt;{identifier = 'gregorian'} / Time Zone: EST (GMT-05:00) offset -18000 Era: 1 Year: 2011 Month: 8 Day: 2 Hour: 9 Minute: 47 Second: 21 Week of Month: 1 Week of Year: 31 Year for Week of Year: 2011 Weekday: 3 Weekday Ordinal: 1 Quarter: 0 </code></pre>
    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.
 

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