直接上代碼:
- (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
}
///根據(jù)用戶輸入的時(shí)間(dateString)確定當(dāng)天是星期幾,輸入的時(shí)間格式 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];
}