Objective-C setting NSDate to current UTC

前端 未结 3 989
忘掉有多难
忘掉有多难 2020-11-29 18:51

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

3条回答
  •  半阙折子戏
    2020-11-29 19:21

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

提交回复
热议问题