1. 程式人生 > >iOS 比較兩個時間差

iOS 比較兩個時間差

- (NSString *)intervalFromLastDate: (NSString *) dateString1  toTheDate:(NSString *) dateString2

{

NSArray *timeArray1=[dateString1 componentsSeparatedByString:@"."];

    dateString1=[timeArray1 objectAtIndex:0];

NSArray *timeArray2=[dateString2 componentsSeparatedByString:@"."];

    dateString2=[timeArray2

objectAtIndex:0];

NSLog(@"%@.....%@",dateString1,dateString2);

NSDateFormatter *date=[[NSDateFormatteralloc] init];

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

NSDate *d1=[date dateFromString:dateString1];

NSTimeInterval late1=[d1 timeIntervalSince1970]*1;

NSDate *d2=[date dateFromString:dateString2];

NSTimeInterval late2=[d2 timeIntervalSince1970]*1;

NSTimeInterval cha=late2-late1;

NSString *timeString=@"";

NSString *house=@"";

NSString *min=@"";

NSString *sen=@"";

    sen = [NSStringstringWithFormat:@"%d", (int)cha%60];

//        min = [min substringToIndex:min.length-7];

//   

    sen=[

NSStringstringWithFormat:@"%@", sen];

    min = [NSStringstringWithFormat:@"%d", (int)cha/60%60];

//        min = [min substringToIndex:min.length-7];

//   

    min=[NSStringstringWithFormat:@"%@", min];

//    小時

    house = [NSStringstringWithFormat:@"%d", (int)cha/3600];

//        house = [house substringToIndex:house.length-7];

    house=[NSStringstringWithFormat:@"%@", house];

    timeString=[NSStringstringWithFormat:@"%@:%@:%@",house,min,sen];

//    return timeString;

return [NSStringstringWithFormat:@"%f",cha];

}