iOS - Friendly NSDate format

前端 未结 9 2201
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 13:31

I need to display the date of posts in my app to the user, right now I do it in this format: \"Fri, 25 May\". How would I format an NSDate to read something like \"2 hours ago\"

9条回答
  •  执笔经年
    2021-02-04 14:24

    +(NSString*)HourCalculation:(NSString*)PostDate
    
    {
        NSLog(@"postdate=%@",PostDate);
        // PostDate=@"2014-04-02 01:31:04";
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        [dateFormat setTimeZone:gmt];
        NSDate *ExpDate = [dateFormat dateFromString:PostDate];
        NSLog(@"expdate=%@",ExpDate);
        NSLog(@"expdate=%@",[NSDate date ]);
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        NSDateComponents *components = [calendar components:(NSDayCalendarUnit|NSWeekCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:ExpDate toDate:[NSDate date] options:0];
    
    //    NSLog(@"year=%d",components.year);
    //    
    //    NSLog(@"month=%d",components.month);
    //    
    //    NSLog(@"week=%d",components.week);
    //    
    //    NSLog(@"day=%d",components.day);
    //    
    //    NSLog(@"hour=%d",components.hour);
    //    
    //    NSLog(@"min=%d",components.minute);
    //    
    //    NSLog(@"sce=%d",components.second);
    //    
    
        NSString *time;
    
        if(components.year!=0)   
        {
            if(components.year==1) 
            {
                time=[NSString stringWithFormat:@"%ld year",(long)components.year];  
            }
            else{
                time=[NSString stringWithFormat:@"%ld years",(long)components.year]; 
            }    
        }
        else if(components.month!=0) 
        {
            if(components.month==1)   
            {
                time=[NSString stringWithFormat:@"%ld month",(long)components.month]; 
            }
            else{
                time=[NSString stringWithFormat:@"%ld months",(long)components.month]; 
            }
          //  NSLog(@"%@",time);
        }
        else if(components.week!=0)
        {
            if(components.week==1)
            {
                time=[NSString stringWithFormat:@"%ld week",(long)components.week];
            }
            else{
                time=[NSString stringWithFormat:@"%ld weeks",(long)components.week];
            }
           // NSLog(@"%@",time);
        }
        else if(components.day!=0)
        {
            if(components.day==1)   
            {
                time=[NSString stringWithFormat:@"%ld day",(long)components.day];
            }
            else{
                time=[NSString stringWithFormat:@"%ld days",(long)components.day]; 
            } 
        }
        else if(components.hour!=0) 
        {
            if(components.hour==1)  
            {
                time=[NSString stringWithFormat:@"%ld hour",(long)components.hour];  
            }
            else{
                time=[NSString stringWithFormat:@"%ld hours",(long)components.hour];
            }
        }
        else if(components.minute!=0)  
        {
            if(components.minute==1)  
            {
                time=[NSString stringWithFormat:@"%ld min",(long)components.minute];
            }
    
            else{
                time=[NSString stringWithFormat:@"%ld mins",(long)components.minute]; 
            }
          //  NSLog(@"time=%@",time);
        }
        else if(components.second>=0){
    
           // NSLog(@"postdate=%@",PostDate);
    
           // NSLog(@"expdate=%@",[NSDate date ]);
    
            if(components.second==0)   
            {
                time=[NSString stringWithFormat:@"1 sec"];
            }
            else{
                time=[NSString stringWithFormat:@"%ld secs",(long)components.second];
            }
        }
        return [NSString stringWithFormat:@"%@ ago",time];
    
    }
    

    This code will show you time in ------------sec like 2 sec ago ------------min like 2 mins ago ------------hours like 2 hours ago ------------days like 2 days ago ------------week like 2 weeks ago ------------month like 2 months ago Lastly.... years like 2 years ago :) try this

提交回复
热议问题