Search code examples
iphoneobjective-ciosnsdateutc

Objective-C setting NSDate to current UTC


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


Solution

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