首页 > 代码库 > [转]根据时间(NSDate)获取具体的信息:月份、星期、天等

[转]根据时间(NSDate)获取具体的信息:月份、星期、天等

[iPhone类代码]计算时间,month,week and day,hour , minutes and second

//initializtion parameter

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now;
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
                                         NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
now=[NSDate date];
comps = [calendar components:unitFlags fromDate:now];
week = [comps weekday];    
month = [comps month];
day = [comps day];
hour = [comps hour];
min = [comps minute];
sec = [comps second];

//Week:
1 --星期天
2--星期一
3--星期二
4--星期三
5--星期四
6--星期五
7--星期六

[转]根据时间(NSDate)获取具体的信息:月份、星期、天等