输入的日期字符串形如:@"1992-05-21 13:08:08"
- (NSDate *)dateFromString:(NSString *)dateString{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
NSDate *destDate= [dateFormatter dateFromString:dateString];
return destDate;
}
- (NSString *)stringFromDate:(NSDate *)date{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
//zzz表示时区,zzz可以删除,这样返回的日期字符将不包含时区信息。
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
NSString *destDateString = [dateFormatter stringFromDate:date];
return destDateString;
}
NSString和NSDate的转换
时间: 2024-11-18 20:11:25