想在程序 开始或者进入某个界面,到结束程序或退出某个界面,获取到这个持续时间.获取到这个时间还需要转化一个取得时分秒. startTime == 全局变量 NSString *startTime; //开始的地方调用 -(NSString*)getCurrentTime{ NSDateFormatter *formatter = [[NSDateFormatteralloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *dateTime = [formatter stringFromDate:[NSDate date]]; startTime = dateTime; returnstartTime; } //结束的地方使用以下代码: NSDateFormatter *formatter= [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date1 = [formatter dateFromString:startTime]; NSDate *date2 = [NSDate date]; NSTimeInterval aTime = [date2 timeIntervalSinceDate:date1]; int hour =(int)(aTime/3600); int minute = (int)(aTime-hour*3600)/60; float second = aTime - hour*3600 - minute*60; NSLog(@"相隔:%f秒",second); if (second>0.2) { }else{ NSLog(@"小于0.2秒"); }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/3280.html