直接上代码:
- (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];
}
网友评论