iOS NSDate与NSString相互转化

1.时间格式的字符串转date


NSString *birthdayStr=@"1986-03-28 00:00:00.000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//解决8小时时间差问题
5 NSDate *birthdayDate = [dateFormatter dateFromString:birthdayStr];

2.date转字符串


NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:date];

你可能感兴趣的:(iOS NSDate与NSString相互转化)