IOS判断日期 昨天/今天



-(NSString *)compareDate:(NSString *)dateStr{
    NSString *pstrText;
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    formatter.dateFormat=@"yyyy-MM-dd";
    NSDate *pDate = [formatter dateFromString:dateStr];
    //NSLog(@"%@===============",pDate);
    formatter.dateFormat = @"MM月dd日";
    NSString *pTempStr = [formatter stringFromDate:pDate];
    NSString *pTempStr2 = [pTempStr substringToIndex:1];
    //NSLog(@"%@----",pTempStr2);
    if ([pTempStr2 isEqualToString: @"0"]) {
        pTempStr = [pTempStr substringFromIndex:1];
    }
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *pDateStr = [NSString stringWithFormat:@"%@ 00:00:00",dateStr];
    NSDate *tempDate = [formatter dateFromString:pDateStr];
    long64 iTime = [[NSString stringWithFormat:@"%.0f", [tempDate timeIntervalSinceNow]] longLongValue];
    int iDays = (int)(-iTime / (3600 * 24));
     if (iDays == 0) {
        pstrText = @"今天";
    }else if(iDays == 1) {
       pstrText = @"昨天";
    }else{
    
       pstrText = pTempStr;
    }
    return pstrText;
    
}

你可能感兴趣的:(IOS判断日期 昨天/今天)