#define TIME_FORMAT_DEFAULT @"yyyy-MM-dd HH:mm:ss"
// 字符串-> NSDate
+(NSDate *)dateFromFormatedString:(NSString *)formatString
{
NSDate *date;
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:TIME_FORMAT_DEFAULT];
date = [formatter dateFromString:formatString];
return date;
}
+(NSDate *)dateFromFormatedString:(NSString *)formatString byFormat:(NSString *)format
{
if(!formatString)
return nil;
NSDate *date;
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:format];
date = [formatter dateFromString:formatString];
return date;
}
// NSDate -> 字符串
+(NSString *)timeDateFormatter:(NSDate *)date type:(int)_type
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
switch (_type) {
case 1:
[dateFormatter setDateFormat:@"yyyy"];
break;
case 2:
[dateFormatter setDateFormat:@"yyyy-MM"];
break;
case 3:
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
break;
case 4:
[dateFormatter setDateFormat:@"hh"];
break;
case 5:
[dateFormatter setDateFormat:@"mm"];
break;
case 6:
[dateFormatter setDateFormat:@"ss"];
break;
case 7:
[dateFormatter setDateFormat:@"a"];
break;
case 8:
[dateFormatter setDateFormat:@"MM"];
break;
case 9:
[dateFormatter setDateFormat:@"dd"];
break;
case 10:
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
break;
case 11:
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
break;
case 12:
[dateFormatter setDateFormat:@"HH:mm:ss"];
break;
case 13:
[dateFormatter setDateFormat:NSLocalizedString(@"yyyy年MM月dd日 HH时mm分", nil)];
break;
case 14:
[dateFormatter setDateFormat:NSLocalizedString(@"yyyy年MM月dd日", nil)];
break;
case 15:
[dateFormatter setDateFormat:@"HH:mm"];
break;
case 16:
[dateFormatter setDateFormat:NSLocalizedString(@"MM月dd日 HH时mm分", nil)];
break;
case 17:
[dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
break;
case 18:
[dateFormatter setDateFormat:@"yyyy.MM.dd HH:mm"];
break;
case 19:
[dateFormatter setDateFormat:NSLocalizedString(@"yyyy年MM月dd日 HH:mm", nil)];
break;
case 20:
[dateFormatter setDateFormat:@"yyyy.MM.dd"];
break;
case 21:
[dateFormatter setDateFormat:NSLocalizedString(@"MM月dd日HH:mm", nil)];
break;
case 22:
[dateFormatter setDateFormat:@"MM-dd HH:mm:ss"];
break;
case 23:
[dateFormatter setDateFormat:NSLocalizedString(@"MM月dd日", nil)];
break;
case 24:
[dateFormatter setDateFormat:@"MM-dd HH:mm"];
break;
case 25:
[dateFormatter setDateFormat:@"MM-dd"];
break;
case 26:
[dateFormatter setDateFormat:@"MM.dd"];
break;
case 27:
[dateFormatter setDateFormat:NSLocalizedString(@"HH时mm分", nil)];
break;
default:
break;
}
NSString *timeString = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:date]];
return timeString;
}