获取两个日期的天数差

日期格式:

day1 - 2018-08-31

day2 - 2018-09-23

#pragma mark - 获取两个日期的天数差  (日期格式yyyy-MM-dd)

+ (NSInteger)getDistanceByDay1:(NSString*)day1 day2:(NSString*)day2 {

    // 将入住日转为时间戳

    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];

    formatter1.dateFormat=@"yyyy-MM-dd";

    NSDate*date1 = [formatter1dateFromString:day1];

    NSInteger timeSp1 = [[NSNumber numberWithDouble:[date1 timeIntervalSince1970]] integerValue];

    // 将离开日转为时间戳

    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];

    formatter2.dateFormat=@"yyyy-MM-dd";

    NSDate*date2 = [formatter2dateFromString:day2];

    NSInteger timeSp2 = [[NSNumber numberWithDouble:[date2 timeIntervalSince1970]] integerValue];

    // 天数差

    NSIntegerdayCount = (timeSp2 - timeSp1) / (24*60*60);


    returndayCount;


}

你可能感兴趣的:(获取两个日期的天数差)