NSDateãæ¥ä»ã«å¤æãã
ã¯ããã«NSDateComponentsã«ä¸åº¦å¤æããå¿ è¦ãããããã®å¤æã«ã¯NSCalendarãå¿ è¦ã¨ããã
- (NSDateComponents *)componentsFromDate:(NSDate *)date withCalendar:(NSCalendar *)calendar { /* calendarãæå®ããã¦ããªãå ´åã«ã¯ãç¾å¨ã®ã«ã¬ã³ãã¼ãæå®ãã */ if (!calendar) { calendar = [NSCalendar currentCalendar]; } /* æ¥ä»ã®ã³ã³ãã¼ãã³ãã¨ãã¦ãå¹´ææ¥æåç§ãåå¾ãã */ static unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; return [calendar components:unitFlags fromDate:date]; }
NSDateComponentsãåå¾å¾ã¯ãä¸è¨ã®ããã«æ¥ä»ãåå¾ããã
NSDate *date = [NSDate date]; NSDateComponents *components = [self componentsFromDate:date withCalendar:nil]; NSLog(@"%då¹´ %dæ %dæ¥ %dæ %då %dç§", [components year], [components month], [components day], [components hour], [components minute], [components second]);