Created
August 15, 2013 03:47
-
-
Save annidy/411b94620257dc7b1588 to your computer and use it in GitHub Desktop.
NSCalendar示例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSDate *date = [NSDate date]; | |
NSCalendar *calendar = [NSCalendar currentCalendar]; | |
NSDateComponents *comps; | |
// 年月日获得 | |
comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) | |
fromDate:date]; | |
NSInteger year = [comps year]; | |
NSInteger month = [comps month]; | |
NSInteger day = [comps day]; | |
NSLog(@"year: %d month: %d, day: %d", year, month, day); | |
//当前的时分秒获得 | |
comps = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) | |
fromDate:date]; | |
NSInteger hour = [comps hour]; | |
NSInteger minute = [comps minute]; | |
NSInteger second = [comps second]; | |
NSLog(@"hour: %d minute: %d second: %d", hour, minute, second); | |
// 周几和星期几获得 | |
comps = [calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) | |
fromDate:date]; | |
NSInteger week = [comps week]; // 今年的第几周 | |
NSInteger weekday = [comps weekday]; // 星期几(注意,周日是“1”,周一是“2”。。。。) | |
NSInteger weekdayOrdinal = [comps weekdayOrdinal]; // 这个月的第几周 | |
NSLog(@"week: %d weekday: %d weekday ordinal: %d", week, weekday, weekdayOrdinal); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment