iOS tips

//判断日期是今天还是昨天还是以前
比如你昨天发了一条说说,我今天看就是显示昨天几时几分,明天看就是2016年7月14日 几时几分

    NSTimeInterval _interval = [_dataArray[i][@"publishDate"] integerValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    [self compareDate:date andModel:model];

-(void)compareDate:(NSDate *)date andModel:(FaceModel *)model {
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDate alloc] init];
NSDate *yesterday;
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];


NSString * todayString = [[today description] substringToIndex:10];
NSString * yesterdayString = [[yesterday description] substringToIndex:10];
NSString * dateString = [[date description] substringToIndex:10];

if ([dateString isEqualToString:todayString])  //今天
{
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    [objDateformat setDateFormat:@"HH:mm"];
    NSString *resultString = [objDateformat stringFromDate:date];
    model.time = resultString;
    
} else if ([dateString isEqualToString:yesterdayString]) //昨天
{
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    [objDateformat setDateFormat:@"HH:mm"];
    NSString *resultString = [objDateformat stringFromDate:date];
    model.time = [NSString stringWithFormat:@"昨天 %@",resultString];
}
else  //以前
{
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    [objDateformat setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *resultString = [objDateformat stringFromDate:date];
    model.time = resultString;
  }
}


/**
 *  版本升级
 */
- (void)versionUpgrade{

    NSDictionary *dic = @{@"mobile" : @"0",
                          @"varsion_code" : @"9"};
[WSQNetWorking networkingPOSTWithActionType:loginAction requestDictionary:dic success:^(NSDictionary *responseObject) {
    if ([responseObject[@"Dxbug"] integerValue] != 1) {
        NSString *message;
        UIAlertController *alertVC;
        if ([responseObject[@"force"] integerValue] == 1) {
            message = @"有新版本,并且为必须升级版本";
            alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        }else{
            message = @"有新版本,是否升级版本?";
            alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            [alertVC addAction:cancelAction];
        }
        
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id"];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            [self versionUpgrade];
        }];
        
        [alertVC addAction:sureAction];
        
        
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];}
    
}
 
                                    failure:^(NSError *error)
 { NSString *description = error.localizedDescription;
     NSString *reason = error.localizedFailureReason;
     NSString *errorMessage = [NSString stringWithFormat:@"%@ %@", description, reason];
     [SVProgressHUD showErrorWithStatus:errorMessage maskType:SVProgressHUDMaskTypeBlack ];
     }];
}



//日期转为年龄
    - (int)dateChangeAge:(NSString *)birth {
//计算年龄
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
//生日
    NSDate *birthDay = [dateFormatter dateFromString:birth];
//当前时间
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
    NSDate *currentDate = [dateFormatter dateFromString:currentDateStr];
    NSTimeInterval time=[currentDate     timeIntervalSinceDate:birthDay];
    int age = ((int)time)/(3600*24*365);
    return age;
}


/**
 *  根据生日计算星座
 *
 *  @param month 月份
 *  @param day   日期
 *
 *  @return 星座名称
 */
- (NSString *)calculateConstellationWithMonth:    (NSInteger)month day:(NSInteger)day
{
    NSString *astroString = @"魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
    NSString *astroFormat = @"102123444543";
    NSString *result = [NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(month*2-(day < [[astroFormat substringWithRange:NSMakeRange((month-1), 1)] intValue] - (-19))*2,2)]];
        return [NSString stringWithFormat:@"%@座",result];
}

你可能感兴趣的:(iOS tips)