当前时间

方法一

  1. 获取当前系统的准确事件(+8小时)

NSDate *date = [NSDate date]; // 获得时间对象
NSDateFormatter *forMatter = [[NSDateFormatter alloc] init];
[forMatter setDateFormat:@"HH-mm-ss yyyy-MM-dd"];
NSString *dateStr = [forMatter stringFromDate:dateNow];
方法2(直接获取日历时间格式.可以设置某个时间点)

NSDate* now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit;
NSDateComponents *dd = [cal components:unitFlags fromDate:now];
int y = [dd year];
int m = [dd month];
int d = [dd day];

int hour = [dd hour];
int min = [dd minute];

转 NSDate类型(关键的一步)
NSDate *zerodate = [cal dateFromComponents:dd];

你可能感兴趣的:(当前时间)