80

Is there an easy way to init an NSDate with the current UTC date/time?

Pandafox
  • 564
  • 1
  • 5
  • 18
Brodie
  • 3,526
  • 8
  • 42
  • 61
  • 1
    Would `[NSDate date]` work? I'm not sure if it's UTC. – zneak Apr 11 '10 at 02:25
  • You want an NSDate set to UTC. (Not sure why everyone is giving you "local date" or "NSString" values.) – Patricia Oct 21 '10 at 19:36
  • 2
    There's no such thing as an `NSDate` set to UTC, it has no timezone associated with it. – Nick Forge Feb 17 '11 at 09:30
  • @NickForge NSDate does have a timezone. From the NSDate class reference: "This method returns a time value relative to an absolute reference date—the first instant of 1 January 2001, GMT." Note the clear and specific reference to GMT. – Murray Sagal Mar 20 '13 at 08:10
  • 3
    @MurraySagal You're incorrect - `NSDate` represents an instant in time (the name `NSDate` is something of a misnomer). When describing a time such as "the first instant of 1 January 2001", you need a time zone to work out what exact instant in time you are talking about, but once the `NSDate` is created, there is no time zone associated with it. – Nick Forge Mar 21 '13 at 10:47
  • 1
    @NickForge An NSDate is created as "the first instant of 1 January 2001, GMT", a specific time in a specific time zone, and that fact is immutable and critically important to why dates work as they do. If we are in different time zones and we logged this at the same moment, `NSLog(@"since1970= %f", [[NSDate date] timeIntervalSince1970]);` the result is the same. Why? Because it's relative to a specific point in time in a specific time zone (GMT), no matter where the user is. The fact that it's relative to GMT does not disappear once it's created. Without this anchor, dates would be arbitrary. – Murray Sagal Mar 21 '13 at 19:29
  • 1
    @MurraySagal Sorry, but your understanding is incorrect. Re-read my last comment. Have you ever wondered why there's no `-timeZone` method on `NSDate`? Or any mention of time zones whatsoever in `NSDate.h`? – Nick Forge Mar 22 '13 at 00:30
  • @NickForge `GMT` is mentioned 19 times in the `NSDate` class reference. There isn't `timeZone` method because it's not required. The documentation is specific, an `NSDate` is relative to a specific point in time in a specific time zone--GMT. Try this. `NSLog(@"%@", [[NSDate date] description]);`. Is that the time where you live? Only if you live in GMT, otherwise it will be off by the number of hours you are from GMT. Now log this `[now descriptionWithLocale:[NSLocale currentLocale]]`. The only reason this works is because the method knows the receiver is anchored to a specific time zone--GMT. – Murray Sagal Mar 22 '13 at 07:34
  • @MurraySagal Nick is correct. There is no such thing as an `NSDate` for a specific timezone because *all* `NSDate`s have a single timezone (GMT). Your quote explains this precisely. Therefore you cannot convert an `NSDate` from one timezone to another. What you really want is to render the *components* of the date for a given timezone. – devios1 Aug 05 '13 at 19:29
  • @chaiguy Throughout these comments Nick has been saying that an NSDate has no timezone associated with it. I've been saying that an NSDate is always in GMT. You seem to be saying that too but you start your comment with "Nick is correct". Can you clarify? – Murray Sagal Aug 06 '13 at 07:30
  • 2
    @MurraySagal You're both right in different ways, but Nick is more right in saying "There's no such thing as an `NSDate` set to UTC", because you can't set the time zone of an `NSDate`, and you're incorrect in saying that it "has a time zone". It's true that it is defined *relative* to a specific time zone, but saying it "has" a time zone implies that time zone is a property of `NSDate`, and that is not true. – devios1 Aug 06 '13 at 19:03
  • 1
    @chaiguy Thank you. I think you've added some needed clarity here. – Murray Sagal Aug 07 '13 at 05:09
  • Nick gets it. Murray is way off. @devious I don't think you quite get it either. What does "defined relative to a specific time zone" even mean? How is NSDate defined relative to a specific time zone? Just because the docs say 1 Jan 2001 GMT?? Well that date time is exactly equivalent to 1 Jan 2001 02:00 Budapest time. So NSDate is exactly defined relative to Budapest time as well then, if you are correct. – Bradley Thomas Sep 05 '16 at 21:41

3 Answers3

148

[NSDate date];

You may want to create a category that does something like this:

-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:localDate];
    [dateFormatter release];
    return dateString;
}
Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
jessecurry
  • 22,068
  • 8
  • 52
  • 44
  • 2
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [dateFormatter setTimeZone:timeZone]; that nailed it thanks – Brodie Apr 11 '10 at 02:46
  • 1
    if this is moving into production you may actually want to create a static dateFormatter that is lazy initialized. Creation of a dateFormatter is actually somewhat expensive. – jessecurry Oct 21 '10 at 20:52
  • 3
    I need it as date object not as string, what can I do in this case? – Maystro Jan 24 '13 at 09:20
  • 1
    Dates exist independent of time zones, the time zone is only used to produce output for human consumption. – jessecurry Jan 24 '13 at 15:13
  • @jessecurry Not quite true. From the NSDate class reference: "This method returns a time value relative to an absolute reference date—the first instant of 1 January 2001, GMT." Note the clear and specific reference to GMT. – Murray Sagal Mar 20 '13 at 08:11
  • Of course, this method shouldn't have "get" in its name. – Nate Chandler May 02 '14 at 18:32
13

NSDate is a reference to an interval from an absolute reference date, January 1, 2001 00:00 GMT. So the class method [NSDate date] will return a representation of that interval. To present that data in a textual format in UTC, just use the NSDateFormatter with the appropriate NSTimeZone (UTC) to render as needed.

rcw3
  • 3,034
  • 1
  • 27
  • 24
1

NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).

Swift version:

extension NSDate {
    func getUTCFormateDate() -> String {
        let dateFormatter = NSDateFormatter()
        let timeZone = NSTimeZone(name: "UTC")
        dateFormatter.timeZone = timeZone
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return dateFormatter.stringFromDate(self)
    }
}
Tikhonov Aleksandr
  • 13,945
  • 6
  • 39
  • 53