1.时间格式的字符串转date
1 NSString *birthdayStr=@"1986-03-28 00:00:00.000"; 2 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 3 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; 4 [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//解决8小时时间差问题 5 NSDate *birthdayDate = [dateFormatter dateFromString:birthdayStr];
2.date转字符串
1 NSDate *date = [NSDate date]; 2 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 3 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 4 NSString *strDate = [dateFormatter stringFromDate:date];