本文主要是介绍Objective-c NSDate 取时间,日期,星期等,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
NSDate
//得到当前的日期
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init] ;
NSInteger unitFlags = NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSWeekdayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit;
comps = [calendar components:unitFlags fromDate:date];
int week = (int)[comps weekday];
int year=(int)[comps year];
int month = (int)[comps month];
int day = (int)[comps day];
int hour = (int)[comps hour];
int min = (int)[comps minute];
int sec = (int)[comps second];
NSLog(@"week%d",week);
NSLog(@"year%d",year);
NSLog(@"month%d",month);
NSLog(@"day%d",day);
NSLog(@"hour%d",hour);
NSLog(@"min%d",min);
NSLog(@"sec%d",sec);
这篇关于Objective-c NSDate 取时间,日期,星期等的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!