oc-利用日历对象编写倒计时模块

NSDateComponents *comps=[[NSDateComponents alloc]init];

[comps setDay:1];

[comps setMonth:8];

[comps setYear:2016];

//创建日历对象

NSCalendar *calender=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

//获得2016-12-5的NSDate日期对象

NSDate *destinationDate=[calender dateFromComponents:comps];

//获得当前日期到2016-12-15时间的NSDateComponents

int unitFlags =NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

NSDateComponents *components=[calender components:unitFlags fromDate:[NSDate date] toDate:destinationDate options:0];

//获得当前日期到2016-12-5相差的天数

int days=(int)[components day];

int shi=(int)[components hour];

int fen=(int)[components minute];

int miao=(int)[components second];

NSMutableAttributedString *attributedTestHolder=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%i天%i时%i分%i秒",days,shi,fen,miao]];

[attributedTestHolder addAttribute:NSFontAttributeName value:[UIFont preferredFontForTextStyle:UIFontTextStyleBody] range:NSMakeRange(attributedTestHolder.length - 1, 1)];

NSLog(@"%@",attributedTestHolder);

你可能感兴趣的:(oc-利用日历对象编写倒计时模块)