根据输入的时间获取当天是星期几
2018-01-11 本文已影响85人
丶小裤头灬
直接上代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"this day is %@", [self getTheDayOfTheWeekByDateString:@"2015-12-18"]);
// Do any additional setup after loading the view, typically from a nib
}
///根据用户输入的时间(dateString)确定当天是星期几,输入的时间格式 yyyy-MM-dd ,如 2015-12-18
-(NSString *)getTheDayOfTheWeekByDateString:(NSString *)dateString{
NSDateFormatter *inputFormatter=[[NSDateFormatter alloc]init];
[inputFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *formatterDate=[inputFormatter dateFromString:dateString];
NSDateFormatter *outputFormatter=[[NSDateFormatter alloc]init];
[outputFormatter setDateFormat:@"EEEE-MMMM-d"];
NSString *outputDateStr=[outputFormatter stringFromDate:formatterDate];
NSArray *weekArray=[outputDateStr componentsSeparatedByString:@"-"];
return [weekArray objectAtIndex:0];
}