美文网首页
iOS日期处理

iOS日期处理

作者: 东岳哥哥 | 来源:发表于2017-12-14 23:32 被阅读0次

    1、yyyy-MM-dd-> 今天、昨天、MM月dd日,注意dateFormater的格式一定要对应上传参过来的日期格式

    + (NSString *)dateToTitleDateString:(NSString *)dateString {
        NSDateFormatter *dateFormater = [NSDateFormatter new];
        dateFormater.dateFormat = @"yyyy-MM-dd";
        NSDate *today = [NSDate date];
        NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];
        NSString *todayString = [dateFormater stringFromDate:today];//今天
        NSString *yestoday = [dateFormater stringFromDate:yesterday];//昨天
        NSString *titleDateString = @"今天";
        if ([dateString isEqualToString:todayString]) {
            titleDateString = @"今天";
        }else if ([dateString isEqualToString:yestoday]){
            titleDateString = @"昨天";
        }else {
            NSDate *tempDate = [dateFormater dateFromString:dateString];
            dateFormater.dateFormat = @"MM月dd日";
            titleDateString = [dateFormater stringFromDate:tempDate];
        }
        return titleDateString;
    }
    

    相关文章

      网友评论

          本文标题:iOS日期处理

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