zoukankan      html  css  js  c++  java
  • IOS日期转为今天昨天形式

    近期项目有类似QQ空间展示动态的UI,模仿了QQ空间的时间显示。在此记录,以备查阅。

    这是QQ空间的ui:

    技术分享

    时间显示为:

      1.今天-->今天 xx:xx(今天 15:39)

      2.昨天-->昨天 xx:xx(昨天 06:00)

      3.前天-->前天 xx:xx(前天 19:00)

      4.同一年, 比如:同一年的一月三号-->01-03 xx:xx(01-03 12:29)

      5.不在同一年 --> xxxx-xx-xx(2014-12-12)

    程序执行效果例如以下。下面为简单的測试。没有ui界面:

    技术分享
    以下的代码是在网上已有代码的基础上更改而来,原代码点此查看
    /**
     *  仿QQ空间时间显示
     *  @param string eg:2015年5月24日 02时21分30秒
     */
    - (NSString *)format:(NSString *)string{
        NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
        [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ];
        [inputFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
        NSDate*inputDate = [inputFormatter dateFromString:string];
        //NSLog(@"startDate= %@", inputDate);
        
        NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init];
        [outputFormatter setLocale:[NSLocale currentLocale]];
        [outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        //get date str
        NSString *str= [outputFormatter stringFromDate:inputDate];
        //str to nsdate
        NSDate *strDate = [outputFormatter dateFromString:str];
        //修正8小时的差时
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate: strDate];
        NSDate *endDate = [strDate  dateByAddingTimeInterval: interval];
        //NSLog(@"endDate:%@",endDate);
        NSString *lastTime = [self compareDate:endDate];
        NSLog(@"lastTime = %@",lastTime);
        return str;
    }
    
    -(NSString *)compareDate:(NSDate *)date{
        
        NSTimeInterval secondsPerDay = 24 * 60 * 60;
        
        //修正8小时之差
        NSDate *date1 = [NSDate date];
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate: date1];
        NSDate *localeDate = [date1  dateByAddingTimeInterval: interval];
        
        //NSLog(@"nowdate=%@
    olddate = %@",localeDate,date);
        NSDate *today = localeDate;
        NSDate *yesterday,*beforeOfYesterday;
        //今年
        NSString *toYears;
        
        toYears = [[today description] substringToIndex:4];
        
        yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
        beforeOfYesterday = [yesterday dateByAddingTimeInterval: -secondsPerDay];
        
        // 10 first characters of description is the calendar date:
        NSString *todayString = [[today description] substringToIndex:10];
        NSString *yesterdayString = [[yesterday description] substringToIndex:10];
        NSString *beforeOfYesterdayString = [[beforeOfYesterday description] substringToIndex:10];
        
        NSString *dateString = [[date description] substringToIndex:10];
        NSString *dateYears = [[date description] substringToIndex:4];
        
        NSString *dateContent;
        if ([dateYears isEqualToString:toYears]) {//同一年
            //今 昨 前天的时间
            NSString *time = [[date description] substringWithRange:(NSRange){11,5}];
            //其它时间
            NSString *time2 = [[date description] substringWithRange:(NSRange){5,11}];
            if ([dateString isEqualToString:todayString]){
                dateContent = [NSString stringWithFormat:@"今天 %@",time];
                return dateContent;
            } else if ([dateString isEqualToString:yesterdayString]){
                dateContent = [NSString stringWithFormat:@"昨天 %@",time];
                return dateContent;
            }else if ([dateString isEqualToString:beforeOfYesterdayString]){
                dateContent = [NSString stringWithFormat:@"前天 %@",time];
                return dateContent;
            }else{
                return time2;
            }
        }else{
            return dateString;
        }
    }
  • 相关阅读:
    最舒适的路线(并查集)
    POJ 2411 状态压缩DP
    NYOJ 708 ones
    HUD 1024 Max Sum Plus Plus
    最长上升子序列
    HDU 4717 The Moving Points
    重新开始写随笔
    读书的意义
    读《如何阅读一本书》笔记
    读《GRAY HAT PYTHON》笔记
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7058639.html
Copyright © 2011-2022 走看看