当前位置: 代码迷 >> Iphone >> 转载iPhone开发31天——第一个例证MinutesToMidnight的时间日期转换代码
  详细解决方案

转载iPhone开发31天——第一个例证MinutesToMidnight的时间日期转换代码

热度:55   发布时间:2016-04-25 06:28:59.0
转载iPhone开发31天——第一个例子MinutesToMidnight的时间日期转换代码
感谢站友liqiang70提供:http://liqiang70.iteye.com/

-(void)updateLabel {
NSDate* now = [NSDate date];
int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];



修改为:

-(void)updateLabel {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit| NSMinuteCalendarUnit |NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];
int hour = 23 - [comps hour];
int min = 59 - [comps minute];
int sec = 59 - [comps second];
countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
}
  相关解决方案