iOS计算两日期相差天数(用字符串格式的时间形式来比较)、将时间戳转为字符串,开始到结束的时间差(用时间戳来比较)

释放双眼,带上耳机,听听看~!
#pragma mark - <计算两日期相差天数>

- (NSString *)compareDaysWithStartDaysStr:(NSString *)startDays withEndDaysStr:(NSString *)endDays

{

    NSLog(@"startDays = %@",startDays);//startDays = 2017-09-19

    NSDateFormatter *formatter = [[NSDateFormatteralloc]init];

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

    NSDate *startDate = [formatterdateFromString:startDays];

    NSDate *endDate   = [formatterdateFromString:endDays];

    NSTimeInterval secondTime = [endDatetimeIntervalSinceDate:startDate];

    int shareDays = secondTime/86400;

    return [NSStringstringWithFormat:@"%d天",shareDays];

}

#pragma mark - <将时间戳转为字符串>

- (NSString *)timeWithTimeIntervalString:(NSString *)timeString

{

    // 格式化时间

    NSDateFormatter* formatter = [[NSDateFormatteralloc]init];

    [formatter setDateFormat:@"MM-dd HH:mm"];

    // 毫秒值转化为秒

    NSDate* date = [NSDatedateWithTimeIntervalSince1970:[timeStringdoubleValue]];

    NSString* dateString = [formatterstringFromDate:date];

    NSLog(@"dateString = %@",dateString);//dateString = 09-29 09:16

    return dateString;

}

/**

 * 开始到结束的时间差,与当前时间相比

 */

- (NSString *)dateTimeDifferenceWithStartTime:(NSString *)time withType:(int)type{//1代表尚可租用时间,2代表已停时间

    NSLog(@"time = %@",time);//time = 1506679200 时间戳

    NSDateFormatter *date = [[NSDateFormatteralloc]init];

    [date setDateFormat:@"MM-dd HH:mm:ss"];

    NSDate *nowD = [[NSDatealloc]init];

    NSTimeInterval timeInterval = [timedoubleValue];

    NSTimeInterval nowInterval = [nowDtimeIntervalSince1970]*1;

    NSTimeInterval value =0.0;

    if (type ==1) {

        value = timeInterval - nowInterval;

    }else{

        value = nowInterval - timeInterval;

    }

    //车停放时间

    //整数

    int integer   = (int)value /3600;

    //余数

    int remainder = (int)value %3600;

    if ( remainder <1800 ) {

        self.stopTime_H = integer +0.5;

    }elseif (remainder <3600){

        self.stopTime_H = integer +1;

    }else{

        self.stopTime_H = integer;

    }

    //时间差对应的天,小时,分钟

    int minute  = (int)value /60%60;

    int hour    = (int)value /3600%24;

    int day     = (int)value /(24*3600)%365;

    NSString *str;

    if (day !=0) {

        str = [NSStringstringWithFormat:@"%d天%d小时%d分钟",day,hour,minute];

    }elseif (day==0 && hour !=0) {

        str = [NSStringstringWithFormat:@"%d小时%d分钟",hour,minute];

    }else{

        str = [NSStringstringWithFormat:@"%d分钟",minute];

    }

    return str;

}

附:

//NSString转NSDate

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

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date = [dateFormatter dateFromString:fin.updatetime];

//NSDate转NSString

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

[Formatter setDateFormat:@"HH:mm"];

NSString *xianshitime = [Formatter stringFromDate:date];

 

人已赞赏
iOS文章

IOS上架此构造版本无效

2021-1-14 17:35:54

iOS文章

iOS开发打印NSRange,CGRect,CGPoint等结构体

2021-1-14 17:37:51

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索