NSDate - convert date to GMT

I need the ability to convert the NSDate value to Date GMT.

How do I convert an NSDate value to an GMT formatted NSDate value, regardless of any date locale setting that iPhone uses?

+50
ios iphone nsdate gmt
Dec 07 '09 at 21:10
source share
5 answers
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [dateFormatter setTimeZone:gmt]; NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; [dateFormatter release]; 
+132
Jan 13
source share

Working with time in Cocoa can be tricky. When you receive an NSDate object, it is in the local time zone. [[NSTimeZone defaultTimeZone] secondsFromGMT] gives you the current time zone offset from GMT. Then you can do this:

 NSDate *localDate = // get the date NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT]; // You could also use the systemTimeZone method NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset; NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval]; 

gmtDate should now have the correct GMT date for you. To display it, see NSDateFormatter , specifically the setDateStyle and setTimeStyle . You create an NSDateFormatter , tweak it the way you want, and then call stringFromDate: to get a beautifully formatted string.

+42
Dec 07 '09 at 21:32
source share

Howard Answer is correct and please vote for it and accept it.

For reference, I think it is useful to explain the difference between date objects and representations of localized dates.

In many programming languages, date objects are used to represent unique points in time. Ignoring the Relativistic arguments, we can assume that in any case, we can determine a point in time that is universally equal for everyone, regardless of how we measure time.

If for each moment in time we could build a unique label, this label could be transmitted and unambiguously indicated. The purpose of date objects is to act as a unique universal label for a given point in time .

You can think of any number of methods for constructing such a marking scheme, and how each date object selects it does not matter to those who use them.

An example would be the use of a numerical offset from a universal event (X seconds after the sun exploded).

Only when we want to take a time point and serialize it into a human-readable line, we have to deal with the complexities of time zones, locales, etc.

( Local date string ) + ( Date formatting ) => Time point

Timepoint + ( Date Formatting ) => ( Local Date String )

Each time point is universal ... there is no such point as a time point , or gmt time point , only after converting the time point to a local string (using date formatting) does it make any connection with the time zone.

Note. I am sure there are a lot of blogs / articles in this thread, but my google foo is failing at this hour. If anyone has the enthusiasm to expand this problem, please feel free to do it.

+16
Aug 10 '10 at 4:37
source share

While Alex's answer was a good start, he did not deal with DST (daylight saving time) and added unnecessary conversion to / from the key date. The following works for me:

To convert from localDate to GMT, taking into account the DST:

 NSDate *localDate = <<your local date>> NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:localDate]; NSDate *gmtDate = [localDate dateByAddingTimeInterval:-timeZoneOffset]; // NOTE the "-" sign! 

To convert from GMT to localDate with DST:

 NSDate *gmtDate = <<your gmt date>> NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:gmtDate]; NSDate *localDate = [gmtDate dateByAddingTimeInterval:timeZoneOffset]; 

One small note: I used dateByAddingTimeInterval, which is only for iOS 4. If you are using OS 3 or earlier, use addTimerInterval.

+5
Aug 09 '10 at 21:46
source share

Have you tried looking at the documentation for NSDateFormatter?

NSDateFormatter

NSDateFormatter seems to have some methods for playing with watches, in particular

-setTimeZone:

I have not tested it myself, but I believe that if you set GMT as a time zone to a date that was originally presented in a different time zone, it will display the date with the correct settings corresponding to the new time zone.

+3
Dec 07 '09 at 21:33
source share



All Articles