美文网首页
iOS - 时间换算

iOS - 时间换算

作者: ShIwEn9 | 来源:发表于2018-12-29 16:27 被阅读0次
    换算时间
    //字符串转成整型
    int pTime = [topicItem.videotime intValue] ;
      int hour = pTime / 3600;
      int minutes = (pTime / 60) % 60 ;
      int second = pTime % 60 ;
      NSString * time = [NSString  stringWithFormat:@"%02d:%02d:%02d" ,hour,minutes,second] ;
    
    获取当前的时间和计算任意时间离当前时间的时间差
     // 当前日期
    NSDate *nowDate = [NSDate date];
    
    // 计算出相差多少秒
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH-mm-ss";
    NSDate *creatData = [formatter dateFromString:(任意时间)];
    // 将传入的字符串转化成时间
    NSTimeInterval timeInterva = [nowDate timeIntervalSinceDate: creatData]; 
    
    
    把数据转换成当前时刻
    NSDate  *date = [NSDate dateWithTimeIntervalSince1970:数据];// 时间戳字符串
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    

    刚刚,几分钟前,几小时前,昨天,前天

    //时间
    NSString *createdTimeStr = @"2017-01-01 21:05:10";
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:createdTimeStr];
    //得到与当前时间差
    NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }else if((temp = timeInterval/60) < 60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }else if((temp = timeInterval/3600) > 1 && (temp = timeInterval/3600) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }else if ((temp = timeInterval/3600) > 24 && (temp = timeInterval/3600) < 48){
        result = [NSString stringWithFormat:@"昨天"];
    }else if ((temp = timeInterval/3600) > 48 && (temp = timeInterval/3600) < 72){
        result = [NSString stringWithFormat:@"前天"];
    }else{
        result = createdTimeStr;
    }
     
    NSLog(@"%@",result);
    
    封装一些简单实用的方法,NSDate的分类
    import "NSDate+Extension.h"
    #import <Foundation/Foundation.h>
    
    @interface NSDate (Extension)
    
    /**
     *传入时间与当前时间的差值
     */
    - (NSDateComponents *)deltaFrom:(NSDate *)date;
    
    /**
     *是否为今年
     */
    - (BOOL)isThisYear;
    
    /**
     *是否为今天
     */
    - (BOOL)isToday;
    
    /**
     *是否为昨天
     */
    - (BOOL)isYestoday;
    
    /**
     *返回只有年月日的时间
     */
    - (NSDate *)dateWithYMD;
    
    import "NSDate+Extension.m"
    #import "NSDate+Extension.h"
    
    @implementation NSDate (Extension)
    
    /**
     传入时间与当前时间的差值
     */
    - (NSDateComponents *)detalFrom:(NSDate *)date{
        
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        //self 发帖时间
        NSDateComponents *compas = [calendar components:unit fromDate:self toDate:date options:0];
        
        return compas;
    }
    
    /**
     *是否为今年
     */
    - (BOOL)isThisYear{
        
        // 日历
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
        NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
        return nowYear == selfYear;
    }
    
    /**
     *是否为今天
     */
    // 第一种方法
    //- (BOOL)isToday{
    //    // 日历
    //    NSCalendar *calendar = [NSCalendar currentCalendar];
    //    NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
    //    NSDateComponents *nowComp = [calendar components:unit fromDate:[NSDate date]];
    //    NSDateComponents *selfComp = [calendar components:unit fromDate:self];
    
    //    return nowComp.year == selfComp.year
    //    && nowComp.month == selfComp.month
    //    && nowComp.day == selfComp.day;
    //}
    
    // 第二种方法
    //- (BOOL)isToday{
    //    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //    formatter.dateFormat = @"yyyy-MM-dd";
    //    NSString *nowString = [formatter stringFromDate:[NSDate date]];
    //    NSString *selfString = [formatter stringFromDate:self];
    //    return nowString == selfString;
    //}
    
    // 第三种
    - (BOOL)isToday{
    // 日历
        NSCalendar *calendar = [NSCalendar currentCalendar];
        return [calendar isDateInToday:self] ;
    }
    
    /**
     *是否为昨天
     */
    - (BOOL)isYestoday{
    //    NSDate *nowDate = [[NSDate date] dateWithYMD];
    //    NSDate *selfDate = [self dateWithYMD];
    //    // 获得nowDate和selfDate的差距、
    //    NSCalendar *calendar = [NSCalendar currentCalendar];
    //    NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    //    return cmps.day == 1;
        
        // 日历
        NSCalendar *calendar = [NSCalendar currentCalendar];
        return [calendar isDateInYesterday:self] ;
        
    }
    
    /**
     *返回只有年月日的时间
     */
    - (NSDate *)dateWithYMD{
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSString *selfString = [formatter stringFromDate:self];
        return [formatter dateFromString:selfString];
    }
    
    @end
    
    实际应用
    //对时间的处理逻辑
        
        //获取发贴时间的年份先进行年份的对比。   ---》 用到了日历对象NSCalendar  类扩展中
        
        NSString *creatTimeStr = topicItem.create_time ;
        
        //发帖日期字符串转为发帖日期对象
        NSDateFormatter *datefmt = [[NSDateFormatter alloc]init] ;
        datefmt.dateFormat = @"yyyy-MM-dd HH:mm:ss" ;//格式不能错误
        //转为发帖日期对象
        NSDate *creatTime = [datefmt dateFromString:creatTimeStr] ;//本质也是遍历和拼接
        
        //获取发帖日期与当前日期的差值
        NSDate *nowData = [NSDate date] ;
        NSDateComponents *detalCmp = [creatTime detalFrom:nowData] ;
        NSLog(@"%@",detalCmp) ;
        if ([creatTime isThisYear]) { // 是今年
            if ([creatTime isToday]) { // 是今天
                if (detalCmp.minute >= 1) { // 几分钟前
                    creatTimeStr = [NSString stringWithFormat:@"%ld分钟前",detalCmp.minute] ;
                }else if (detalCmp.hour >= 1){ // 几小时前
                    creatTimeStr = [NSString stringWithFormat:@"%ld小时前",detalCmp.hour] ;
                }else{ // 刚刚
                    creatTimeStr = @"刚刚";
                }
            }else if ([creatTime isYestoday]){ // 是昨天
                NSLog(@"不是今天") ;
                datefmt.dateFormat = @"昨天 HH:mm" ;
                creatTimeStr = [datefmt stringFromDate:creatTime] ;
            }else{   //昨天之前
                NSLog(@"是昨天") ;
                datefmt.dateFormat = @"MM-dd HH:mm" ;
                creatTimeStr = [datefmt stringFromDate:creatTime] ;
            }
        }else{
            creatTimeStr = topicItem.create_time ;
        }
        
        self.creatTime.font = [UIFont systemFontOfSize:15.f] ;
        
        self.creatTime.text = creatTimeStr ;
        
        
    }
    
    常用的类型转换(简述)
    数据类型转换:
    1)int-->NSNumber:numberWithInt
       NSNumber *numObj = [NSNumber numberWithInt: 2];
    2)NSNumber-->nsinteger:integerValue
    3)string -->double:initWithString
    4)CGFloat --> dobule:initWithFloat,decimalobj doubleValue
    5)NSString与NSInteger的相互转换
       int pTime = [topicItem.videotime intValue] ;  或者 integerValue
    
    

    相关文章

      网友评论

          本文标题:iOS - 时间换算

          本文链接:https://www.haomeiwen.com/subject/yjhplqtx.html