1. 顯示當前的年月日+時間
- NSDate* date = [NSDate date];
- NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
- [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; //指定格式
-
- NSString* timeStr = [formatter stringFromDate:date]; //獲取格式字符串
2. 只顯示當前的日期格式
- NSDate* date = [NSDate date];
- NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
- [formatter setDateFormat:@"yyyy-MM-dd"];
-
- NSString* timerStr = [formatter stringFromDate:date];
3. 獲取某段時間差,可以精確到微秒級別, 用於計算代碼執行效率
- //可以精確到微秒級別
- uint64_t start = clock();
- [_image drawInRect:CGRectMake(0, 0, 400, 300)];
- uint64_t end = clock();
- uint64_t drawTime = end - start;