iOS 解析RFC3339时间格式

https://www.ietf.org/rfc/rfc3339.txt

https://zhuanlan.zhihu.com/p/31829454

-(NSString *)rfc3339DateFormatterStringToUTC8EndMicrosecond;{

//    2020-05-22T12:42:39.336702Z 格林尼治时间转本地时间  时间格式转换

    NSString *result = [NSString stringWithFormat:@"%@Z",[self substringToIndex:self.length - 8]] ;

    NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];

    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

    

    [rfc3339DateFormatter setLocale:enUSPOSIXLocale];

    [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];

    [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    

//    NSDate对象会自动根据当前时区加减时间 从而展示为当前时间

    NSDate *createdAtZero = [rfc3339DateFormatter dateFromString:result];

    NSDateFormatter *showDateFormatter = [[NSDateFormatter alloc] init];

    //    2019/11/16 23:12:34

    [showDateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];

    result = [showDateFormatter stringFromDate:createdAtZero];

    return result;

}

posted @ 2020-06-15 15:58  怀达  阅读(770)  评论(0编辑  收藏  举报