Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting string to NSDate, timezones do not adjust correctly?
    primarykey
    data
    text
    <p>I'm trying to convert a time string from one timezone into an NSDate object, and then output it using the local timezone back to a string. However I seem to be going 1 hour out. For an example, I've added in the output timezone so that I can show the error. There are a few similar questions but nothing gives a hint to where I am going wrong with this one!!! Both timezones are currently in daylight savings at the time of posting this - however NSDateFormatter should take care of any difference based upon the timezone.</p> <p><strong>UPDATE:</strong> The issue seems to be with converting the LA time into GMT, not onwards to BST. All NSDate's are stored as GMT as far as I'm aware. The below code demonstrates this (with locale added - doesn't seem to make a difference):</p> <pre><code>NSDateFormatter *dateF = [[NSDateFormatter alloc] init]; [dateF setDateFormat:@"HH:mm"]; [dateF setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]]; [dateF setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; NSDate* sourceDate = [dateF dateFromString:@"09:15"]; NSLog(@"Date GMT: %@", [sourceDate description]); NSLog(@"Date BST: %@", [sourceDate descriptionWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]]); </code></pre> <p><strong>Output:</strong></p> <p>Date GMT: 1970-01-01 17:15:00 +0000 <em>this is incorrect!</em></p> <p>Date BST: Thursday, 1 January 1970 18:15:00 GMT+01:00 <em>thus this is incorrect too</em></p> <p><strong>ORIGINAL EXAMPLE</strong></p> <p>09:15 in LA should be 17:15 in London, not 18:15...</p> <pre><code>NSDateFormatter *dateF = [[NSDateFormatter alloc] init]; [dateF setDateFormat:@"HH:mm"]; [dateF setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]]; NSDate* sourceDate = [dateF dateFromString:@"09:15"]; NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"HH:mm"]; [dateformatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]]; NSLog(@"%@", [dateformatter stringFromDate:sourceDate]); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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