菜鸟成长记-NSDate、NSDateComponents使用

NSDate的一些基本属性我就不介绍了,这里我会讲解如何分解时间,比较时间

直接上代码解释

1:  第一次用此方法实例化对象后,即使修改了系统日历设定,这个对象也不会改变

            NSCalendar * calendar = [NSCalendar currentCalendar];

2  现在通过一个NSDate对象获取到一个NSDateComponent对象     

     //NSDateComponents 日历化组件 为我们提供了year。day。 hours。。。属性
     NSDateComponents * dateCompontents = [calendar components:(NSCalendarUnitYear |NSCalendarUnitMonth |          NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond) fromDate:[NSDate date]];

3这时候你就可以得到所有属性值了      

        NSLog(@"%ld",dateCompontents.year);
        NSLog(@"%ld",dateCompontents.month);
        NSLog(@"%ld",dateCompontents.day);
        NSLog(@"%ld",dateCompontents.hour);
        NSLog(@"%ld",dateCompontents.minute);
        NSLog(@"%ld",dateCompontents.second);


4:获取两个时间之间的间隔  

NSDateComponents * betwenDateCompontent = [calendar components:NSCalendarUnitYear |NSCalendarUnitMonth        fromDate:since1970 toDate:since2001 options:NSCalendarWrapComponents];

5:时间格式化  完成字符串和NSDate对象之间的相互转换     

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    //设置时区  不会相差八小时
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    //设置语言
    [formatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"]];
    formatter.dateFormat =@"yyyy-MM-dd HH:mm:ss";
    //字符串转日期
     NSDate * forMatterDate = [formatter dateFromString:@"2001-01-01 00:00:00"];
    // 日期转字符串
     NSString * forMatterStr = [formatter stringFromDate:[NSDate date]];
     NSLog(@"%@",forMatterDate);
     NSLog(@"%@",forMatterStr);

6:计算两个日期之前相差的秒数

+ (NSString *)dateTime:(NSString *)startTime endTime:(NSString *)endTime{
    NSDateFormatter *date = [[NSDateFormatter alloc]init];
    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *startD =[date dateFromString:startTime];
    NSDate *endD = [date dateFromString:endTime];
    NSTimeInterval start = [startD timeIntervalSince1970]*1;
    NSTimeInterval end = [endD timeIntervalSince1970]*1;

    
    NSTimeInterval value = end - start;
     NSLog(@"相差秒数value:%f",value);
    return [NSString stringWithFormat:@"耗时%f秒",value];


    {
    int second = (int)value %60;//秒
    int minute = (int)value /60%60;
    int house = (int)value / (24 * 3600)%3600;
    int day = (int)value / (24 * 3600);
    NSString *str;
    if (day != 0) {
        str = [NSString stringWithFormat:@"耗时%d天%d小时%d分%d秒",day,house,minute,second];
    }else if (day==0 && house != 0) {
        str = [NSString stringWithFormat:@"耗时%d小时%d分%d秒",house,minute,second];
    }else if (day== 0 && house== 0 && minute!=0) {
        str = [NSString stringWithFormat:@"耗时%d分%d秒",minute,second];
    }else{
        str = [NSString stringWithFormat:@"耗时%d秒",second];
    }
    return str;
    }
}



 

相信到这里大家应该NSDate有了一定印象,就上面的知识点掌握之后,你完全可以实现刷新网络数据显示刷新间隔时间 这里提供一个封装好的方法判断是否是今天。

 

- (BOOL)isToday
 {
    NSCalendar *calendar = [NSCalendarcurrentCalendar];
    int unit =NSCalendarUnitDay |NSCalendarUnitMonth | NSCalendarUnitYear;
    
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    
    return
    (selfCmps.year == nowCmps.year) &&
    (selfCmps.month == nowCmps.month) &&
    (selfCmps.day == nowCmps.day);
 }

 相信判断是否是今天,今天大家都应该会了,那接下来就是更实用的了,判断数据是刚刚、还是多少分钟前、还是多少小时前、等等

    

  if (服务器时间.isToday) {  是否是今天

     NSCalendar *calendar = [NSCalendarcurrentCalendar];
     int unit =NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
     NSDateComponents *now = [calendar components:unit fromDate:服务器时间 toDate:[NSDate date] options:0];
        if  (now.hour>=1){       
          return [NSStringstringWithFormat:@"%ld小时前",now.hour];
        }else if (now.minute>=1){
            return [NSStringstringWithFormat:@"%ld分钟前",now.minute];
        }else{
            return @"刚刚";
        }
    }

        

好了,写到这里一些项目中实用的功能就可以实现了




你可能感兴趣的:(iOS-进击之路)