美文网首页
两个时间的时间差

两个时间的时间差

作者: _AmoJason | 来源:发表于2017-08-22 16:49 被阅读0次

    -(NSString*)timeIntervalFromNextTime:(NSString*)nextTime

    nextTimeFormat:(NSString*)format1

    ToCurrentTime:(NSString*)currentTime

    currentTimeFormat:(NSString*)format2{

    //下次时间

    NSDateFormatter*dateFormatter1 = [[NSDateFormatteralloc]init];

    dateFormatter1.dateFormat= format1;

    NSDate*nextDate = [dateFormatter1dateFromString:nextTime];

    //当前时间

    NSDateFormatter*dateFormatter2 = [[NSDateFormatteralloc]init];

    dateFormatter2.dateFormat= format2;

    NSDate*currentDate = [dateFormatter2dateFromString:currentTime];

    return[selftimeIntervalFromNextTime:nextDateToCurrentTime:currentDate];

    }

    -(NSString*)timeIntervalFromNextTime:(NSDate*)nextTime ToCurrentTime:(NSDate*)currentTime{

    NSTimeZone*timeZone = [NSTimeZonesystemTimeZone];

    //上次时间

    NSDate*nextDate = [nextTimedateByAddingTimeInterval:[timeZonesecondsFromGMTForDate:nextTime]];

    //当前时间

    NSDate*currentDate = [currentTimedateByAddingTimeInterval:[timeZonesecondsFromGMTForDate:currentTime]];

    //时间间隔

    NSIntegerintevalTime = [nextDatetimeIntervalSinceReferenceDate]-[currentDatetimeIntervalSinceReferenceDate];

    //秒、分、小时、天、月、年

    NSIntegerminutes = intevalTime /60;

    NSIntegerhours = intevalTime /60/60;

    if(minutes <60)

    {

    if(minutes <0)

    {

    //为负数时,用一天的时间减去当前过去的时间。等于总分钟数

    NSIntegerallMinutes = (minutes +24*60);

    //当总分钟数小于60时,显示多少分钟后喝奶

    if(allMinutes<60)

    {

    return[NSStringstringWithFormat:@"%ld分钟后\n需要喝奶",(long)allMinutes];

    }

    else

    {

    //大于60时,换算成小时来算。几个小时后喝奶

    NSIntegerhours = (minutes +24*60)/60;

    return[NSStringstringWithFormat:@"%ld小时后\n需要喝奶",(long)hours];

    }

    }

    else

    {

    return[NSStringstringWithFormat:@"%ld分钟后\n需要喝奶",(long)minutes];

    }

    }

    elseif(hours <24)

    {

    return[NSStringstringWithFormat:@"%ld小时后\n需要喝奶",(long)hours];

    }

    else{

    }

    return@"";

    }

    相关文章

      网友评论

          本文标题:两个时间的时间差

          本文链接:https://www.haomeiwen.com/subject/ovmldxtx.html