NSString转化成NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date=[formatter dateFromString:dataString];
将某一字符串直接转换成NSDate,会发现实际结果与原时间相差八小时。这是因为NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间:
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeDate = [date dateByAddingTimeInterval: interval];