美文网首页
iOS 和当前时间进行比较 输出字符串为(刚刚几个小时前 几天

iOS 和当前时间进行比较 输出字符串为(刚刚几个小时前 几天

作者: hanyongwei | 来源:发表于2018-08-10 16:07 被阅读0次
    /**
      和当前时间进行比较  输出字符串为(刚刚几个小时前 几天前 )
      最好要传入的时间格式 2017-06-14 14:18:54
     @param timeStr 输入时间
     @param format 时间格式,默认为yyyy-MM-dd HH:mm:ss
     @return 输出字符串为(刚刚几个小时前 几天前 )
     */
    +(NSString *)inputTimeStr:(NSString *)timeStr withFormat:(NSString *)format
    
    {
        
        NSDate *nowDate = [NSDate date];
        
        NSDate *sinceDate = [self becomeDateStr:timeStr withFormat:format];
        
        int i  = [nowDate timeIntervalSinceDate:sinceDate];
        
        
        
        NSString  *str  = @"";
        
        
        
        if (i <= 60)
            
        {//小于60s
            
            str = @"刚刚";
            
        }else if(i>60 && i<=3600)
            
        {//大于60s,小于一小时
            
            str = [NSString stringWithFormat:@"%d分钟前",i/60];
            
        }else if (i>3600 && i<60*60*24)
            
        {//
            
            if ([self isYesterdayWithDate:sinceDate])
                
            {//24小时内可能是昨天
                
                str = [NSString stringWithFormat:@"昨天"];
                
            }else
                
            {//今天
                
                str = [NSString stringWithFormat:@"%d小时前",i/3600];
                
            }
            
        }else
            
        {//
            
            int k = i/(3600*24);
            
            if ([self isYesterdayWithDate:sinceDate])
                
            {//大于24小时也可能是昨天
                
                str = [NSString stringWithFormat:@"昨天"];
                
            }else
                
            {
                
                //在这里大于1天的我们可以以周几的形式显示
                
                if (k>=1)
                    
                {
                    
                    if (k < [self getNowDateWeek])
                        
                    {//本周
                        
                        str  = [self weekdayStringFromDate:[self becomeDateStr:timeStr withFormat:format]];
                        
                    }else
                        
                    {//不是本周
                        
                        //                    str  = [NSString stringWithFormat:@"不是本周%@",[self weekdayStringFromDate:[self becomeDateStr:timeStr]]];
                        
                        str  = timeStr;
                        
                    }
                    
                }else
                    
                {//
                    
                    str = [NSString stringWithFormat:@"%d天前",i/(3600*24)];
                    
                }
                
            }
            
        }
        
        return str;
        
    }
    
    //把时间字符串转换成NSDate
    
    + (NSDate *)becomeDateStr:(NSString *)dateStr withFormat:(NSString *)format
    
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        
        if (format) {
            [dateFormatter setDateFormat:format];
        }else{
             [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        }
        
        NSDate *date1 = [dateFormatter dateFromString:dateStr];
        
        return date1;
        
    }
    
    //把时间转换成星期
    
    + (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
        
        
        
        NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
        
        
        
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        
        
        
        //    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
        
        NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"zh-Hans"];
        
        
        
        [calendar setTimeZone: timeZone];
        
        
        
        NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
        
        
        
        NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
        
        
        
        return [weekdays objectAtIndex:theComponents.weekday];
        
    }
    
    //判断是否为昨天
    
    + (BOOL)isYesterdayWithDate:(NSDate *)newDate
    
    {
        
        BOOL isYesterday = YES;
        
        NSTimeInterval secondsPerDay = 24 * 60 * 60;
        
        //
        
        NSDate *yearsterDay =  [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay];
        
        /** 前天判断
         
         //    NSDate *qianToday =  [[NSDate alloc] initWithTimeIntervalSinceNow:-2*secondsPerDay];
         
         //    NSDateComponents* comp3 = [calendar components:unitFlags fromDate:qianToday];
         
         //    if (comp1.year == comp3.year && comp1.month == comp3.month && comp1.day == comp3.day)
         
         //    {
         
         //        dateContent = @"前天";
         
         //    }
         
         **/
        
        NSCalendar* calendar = [NSCalendar currentCalendar];
        
        unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
        
        //    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
        
        NSDateComponents* comp1 = [calendar components:unitFlags fromDate:newDate];
        
        NSDateComponents* comp2 = [calendar components:unitFlags fromDate:yearsterDay];
        
        
        
        if ( comp1.year == comp2.year && comp1.month == comp2.month && comp1.day == comp2.day)
            
        {
            
            isYesterday = YES;
            
        }else
            
        {
            
            isYesterday = NO;
            
        }
        
        return isYesterday;
        
    }
    
    //判断今天是本周的第几天
    
    + (int)getNowDateWeek
    
    {
        
        NSDate *nowDate = [NSDate date];
        
        NSString *nowWeekStr = [self weekdayStringFromDate:nowDate];
        
        int  factWeekDay = 0;
        
        
        
        if ([nowWeekStr isEqualToString:@"周日"])
            
        {
            
            factWeekDay = 7;
            
        }else if ([nowWeekStr isEqualToString:@"周一"])
            
        {
            
            factWeekDay = 1;
            
        }else if ([nowWeekStr isEqualToString:@"周二"])
            
        {
            
            factWeekDay = 2;
            
        }else if ([nowWeekStr isEqualToString:@"周三"])
            
        {
            
            factWeekDay = 3;
            
        }else if ([nowWeekStr isEqualToString:@"周四"])
            
        {
            
            factWeekDay = 4;
            
        }else if ([nowWeekStr isEqualToString:@"周五"])
            
        {
            
            factWeekDay = 5;
            
        }else if ([nowWeekStr isEqualToString:@"周六"])
            
        {
            
            factWeekDay = 6;
            
        }
        
        return  factWeekDay;
        
    }
    ```

    相关文章

      网友评论

          本文标题:iOS 和当前时间进行比较 输出字符串为(刚刚几个小时前 几天

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