//获取当前时间
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
NSLog(@"dateString:%@",dateString);
获取时间差
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm:ss"];
self.endTime = [dateFormatter stringFromDate:currentDate];
NSDate *date1 = [dateFormatter dateFromString:self.beginTime];
NSDate *date2 = [dateFormatter dateFromString:self.endTime];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 4.利用日历对象比较两个时间的差值
NSDateComponents *cmps = [calendar components:type fromDate:date1 toDate:date2 options:0];
long a =cmps.minute/10;
int b=cmps.minute%10;
long c =cmps.second/10;
int d= cmps.second%10;
self.closelabel.text =getStr(self.hidName);
self.liveTime.text=[NSString stringWithFormat:@"%ld%d:%ld%d",a,b,c,d];
NSLog(@"两个时间相差%ld年%ld月%ld日%ld小时%ld分钟%ld秒", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);
网友评论