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

4 篇文章 0 订阅

#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;

}

 

 

 

附:

//NSStringNSDate

NSDateFormatter *dateFormatter = [[NSDateFormatter allocinit];

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

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

 

//NSDate转NSString

 

NSDateFormatter *Formatter = [[NSDateFormatter allocinit];

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

NSString *xianshitime = [Formatter stringFromDate:date];

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值