产品有个需求,显示指定日期格式,这个日期又特么是后端胡乱给的,没法子,自己转,直接上代码
- (NSString *)getDate:(NSString *)date withDateStyle(NSString *)dateStyle
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
NSLocale *local=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:local];//需要配置区域,不然会造成模拟器正常,真机日期为null的情况
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];// 设置源时间字符串的格式
NSDate *date1 = [formatter dateFromString:date];
// NSLog(@"date--%@___class---%@",date1,[date1 class]);
NSDateFormatter* toformatter = [[NSDateFormatter alloc] init];
[toformatter setDateStyle:NSDateFormatterMediumStyle];
[toformatter setTimeStyle:NSDateFormatterShortStyle];
[toformatter setDateFormat: dateStyle];//设置目标时间字符串的格式
NSString *targetTime = [toformatter stringFromDate:date1];//将时间转化成目标时间字符串
XHLog(@"%@",targetTime);
return targetTime;
}
就这些,那好不送