cool hit counter How many days, hours, minutes and seconds do the two times differ for ios_Intefrankly

How many days, hours, minutes and seconds do the two times differ for ios


Today used a time class, previously written but can not find a moment in this record to facilitate future viewing.

/**
 How many days, hours, minutes and seconds are the difference between the two times

 @param startTime  Start time
 @param endTime  Closing time
 @return  Time difference
 */
+ (NSString *)dateTimeDifferenceWithStartTime:(NSString *)startTime endTime:(NSString *)endTime
{
    
    NSDateFormatter *date = [[NSDateFormatter alloc]init];
    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *startDate =[date dateFromString:startTime];
    NSDate *endDdate = [date dateFromString:endTime];
    
    NSTimeInterval startInterval = [startDate timeIntervalSince1970]*1;
    NSTimeInterval endInterval = [endDdate timeIntervalSince1970]*1;
    NSTimeInterval value = endInterval - startInterval;
    
    //  day
    int day = (int)value / (24 *3600);
    //  hours
    int house = (int)value / (24 *3600)%3600;
    //  a point (in sports or games)
    int minute = (int)value /60%60;
    //  second
    int second = (int)value %60;
    
    NSString *timeStr;
    
    if (day != 0) {
        timeStr = [NSString stringWithFormat:@"%d day%d hours%d a point (in sports or games)%d second",day,house,minute,second];
    }
    else if (day==0 && house !=0) {
        timeStr = [NSString stringWithFormat:@"%d hours%d a point (in sports or games)%d second",house,minute,second];
    }
    else if (day==0 && house==0 && minute!=0) {
        timeStr = [NSString stringWithFormat:@"%d a point (in sports or games)%d second",minute,second];
    }
    else{
        timeStr = [NSString stringWithFormat:@"%d second",second];
    }
    
    return timeStr;
}

Method II

- (NSString *)dateTimeDifferenceWithStartTime:(NSString *)startTime endTime:(NSString *)endTime
{
    NSDateFormatter *date = [[NSDateFormatter alloc]init];
    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *startDate =[date dateFromString:startTime];
    NSDate *endDdate = [date dateFromString:endTime];
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponents = [cal components:unitFlags fromDate:startDate toDate:endDdate options:0];
    
    //  day
    NSInteger day = [dateComponents day];
    //  hours
    NSInteger house = [dateComponents hour];
    //  a point (in sports or games)
    NSInteger minute = [dateComponents minute];
    //  second
    NSInteger second = [dateComponents second];
    
    NSString *timeStr;
    
    if (day != 0) {
        timeStr = [NSString stringWithFormat:@"%zd day%zd hours%zd a point (in sports or games)%zd second",day,house,minute,second];
    }
    else if (day==0 && house !=0) {
        timeStr = [NSString stringWithFormat:@"%zd hours%zd a point (in sports or games)%zd second",house,minute,second];
    }
    else if (day==0 && house==0 && minute!=0) {
        timeStr = [NSString stringWithFormat:@"%zd a point (in sports or games)%zd second",minute,second];
    }
    else{
        timeStr = [NSString stringWithFormat:@"%zd second",second];
    }
    
    return timeStr;
}

Recommended>>
1、Googles densehash notes
2、Five elements of the data monetization strategy
3、IndustryEcontracts help the housing rental industry sign up easily and efficiently
4、Installing gcc and its dependencies
5、Java Automatic boxing unboxing and traversal loops

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号