views:

853

answers:

3

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

+6  A: 

[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;
}
jessecurry
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];[dateFormatter setTimeZone:timeZone];that nailed it thanks
Brodie
don't forget to release the date formatter...
Dave DeLong
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
+1  A: 

NSDate is a reference to an interval from an absolute reference date, January 1, 2001 00:00 GMT. So the class method [NSDate now] 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
A: 

You want an NSDate set to UTC. (Not sure why everyone is giving you "local date" or "NSString" values.)

Patricia