IOS中由日期生成星期的一种方法

 1 - (NSInteger)getWeekDataWithYear:(NSString *)year month:(NSString *)month day:(NSString *)day{
 2    
 3     // 创建并设置NSDateComponents,它可以根据制定的时间生成对应的Date,同时需要根据相应的Calender
 4     NSDateComponents *comps = [[NSDateComponents alloc] init];
 5     [comps setDay:[day integerValue]];
 6     [comps setMonth:[month integerValue]];
 7     [comps setYear:[year integerValue]];
 8     // 创建Calender,格林威治时间,周日weekday是1以此类推
 9     NSCalendar *gregorian1 = [[NSCalendar alloc]
10                               initWithCalendarIdentifier:NSGregorianCalendar];
11     NSDate *date = [gregorian1 dateFromComponents:comps];
12     NSDateComponents *weekdayComponents =
13     [gregorian1 components:NSWeekdayCalendarUnit fromDate:date];
14     
15     NSInteger weekday = [weekdayComponents weekday];
16     
17     return weekday;
18 
19 }

 

posted @ 2015-12-20 11:49  击水湘江  阅读(267)  评论(0编辑  收藏  举报