Objective-C setting NSDate to current UTC

Brodie picture Brodie · Apr 11, 2010 · Viewed 64.6k times · Source

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

Answer

jessecurry picture jessecurry · Apr 11, 2010

[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;
}