歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iPhone 獲取指定格式的時間和日期

iPhone 獲取指定格式的時間和日期

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