Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 194 Vote(s) - 3.72 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NSDate - Convert Date to GMT

#1
I need the ability to convert an `NSDate` value to a GMT Date.

How can I go about converting an `NSDate` value to a GMT formatted `NSDate` value, independent of whatever date locale settings the iPhone device is using?
Reply

#2
```
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];
```
Reply

#3
**Swift 4**:

//UTC or GMT ⟺ Local

extension Date {

// Convert local time to UTC (or GMT)
func toGlobalTime() -> Date {
let timezone = TimeZone.current
let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}

// Convert UTC (or GMT) to local time
func toLocalTime() -> Date {
let timezone = TimeZone.current
let seconds = TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}

}
Reply

#4
**[Howard's Answer][1] is correct and please vote it up and accept it.**

For reference I think it is useful to explain the difference between date objects and localised date representations are.

In many programming languages date objects are used to represent unique points in time. Ignoring [Relativistic][2] arguments it can be assumed that at any instance we can define a point in time which is equal universally for every one, regardless of how we measure time.

If for each point in time we could construct a unique label, that label could be passed around and referenced unambiguously. **The purpose of date objects is to act as a unique universal label for a given point in time**.

One could come up with any number of techniques to construct such a labelling scheme and how each date object chooses to do so is immaterial to anyone using them.

An example may be to use a numeric offset from a universal event (_X seconds since the sun exploded_).

It is only when we wish to take a time point and serialize it into a human readable string that we must deal with the complexities of time zones, locales, etc...

(**Local Date String**) + (**Date Formatter**) => Time Point

Time Point + (**Date Formatter**) => (**Local Date String**)

Every time point is universal... there is no such thing as a **new york time point**, or **gmt time point**, only once you convert a time point to a local string (using a date formatter) does any association to a time zone appear.

**Note: I'm sure there are many blogs/articles on this very issue, but my google foo is failing me at this hour. If anyone has the enthusiasm to expand on this issue please feel free to do so.**

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
While Alex's answer was a good start, it didn't deal with DST (daylight savings time) and added an unnecessary conversion to/from the reference date. The following works for me:

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

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

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

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


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

#6
Have you tried looking at the documentation for NSDateFormatter?

[NSDateFormatter][1]

NSDateFormatter appears to have some methods for playing with TimeZones, particularly

`-setTimeZone:`

I haven't tested it myself, but I imagine that if you set GMT as the timezone on a date that is originally represented in another timezone, it will display the date with the correct adjustments to match the new timezone.


[1]:
Reply

#7
Working with time in Cocoa can be complicated. When you get an `NSDate` object, it's in the local time zone. `[[NSTimeZone defaultTimeZone] secondsFromGMT]` gives you the offset of the current time zone 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];

Now `gmtDate` should have the correct date in GMT for you. In order to display it, look at `NSDateFormatter`, specifically the `setDateStyle` and `setTimeStyle` methods. You create an `NSDateFormatter`, configure it the way you want, and then call `stringFromDate:` to get a nicely formatted string.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through