ios计算时间差

//模拟windows GetTickCount //

//#import

+ (int)mxGetTickCount;

{

   int timeTick;

   mach_timebase_info_data_t info;

   uint64_t machineTime;

   mach_timebase_info(&info);

    machineTime =mach_absolute_time();

    timeTick = machineTime * info.numer / info.denom /1000000LL;

    

   return timeTick;

}


// NSDate timeIntervalSinceDate:

//下面转换NSString,用在保存时间时使用。

- (double)mxGetStringTimeDiff:(NSString*)timeS timeE:(NSString*)timeE

{

    double timeDiff = 0.0;

    

    NSDateFormatter *formatters = [[NSDateFormatteralloc]init];    

    [formatterssetDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];

    NSDate *dateS = [formatters dateFromString:timeS];

    

    NSDateFormatter *formatterE = [[NSDateFormatteralloc]init];    

    [formatterEsetDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];

    NSDate *dateE = [formatterE dateFromString:timeE];           

    

    timeDiff = [dateE timeIntervalSinceDate:dateS ];

    

    return timeDiff;

}


- (NSString*)mxGetCurTime

{

   NSDateFormatter *formatter = [[NSDateFormatteralloc]init];

    [formattersetDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];   

    NSString*timeString=[formatter stringFromDate: [NSDatedate]];

    [formatterrelease];

    return timeString;

}

你可能感兴趣的:(objective-c)