2014年3月21日金曜日

【Objective-C】日付(年月日)、時間(時分秒)を求める


- (void) test
{
    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];
    
    // 時分秒
    comps = [calendar components:(NSHourCalendarUnit
                                  | NSMinuteCalendarUnit
                                  | NSSecondCalendarUnit)
                        fromDate:date];
    NSInteger hour   = [comps hour];
    NSInteger minute = [comps minute];
    NSInteger second = [comps second];
    
    NSString *logs;
    //%03d
    logs = [NSString stringWithFormat:@"%d%02d%02d%02d%02d%02d",year,month,day,hour,minute,second];
    NSLog(@"%@",logs);

}

0 件のコメント:

コメントを投稿