ios 获取当前时间

1.第一种返回的时间是一个整个的字符串。

     NSDate *timeDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSString *locationString = [dateFormatter stringFromDate:timeDate];
return locationString;

2.第二种是分离出单个时间点,然后拼接字符串返回(星期也有,这里没有写)。

    NSDate *timeDate = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *conponent = [cal components:unitFlags fromDate:timeDate]; NSInteger year = [conponent year];
NSInteger month = [conponent month];
NSInteger day = [conponent day];
NSInteger hour = [conponent hour];
NSInteger min = [conponent minute];
NSInteger sec = [conponent second]; NSString *nsDateString= [NSString stringWithFormat:@"%4ld-%2ld-%2ld %2ld:%2ld:%2ld", year, month, day,hour, min, sec];
return nsDateString;
上一篇:ABP中mapping中忽略属性


下一篇:个人附加作业XD --这门课终于结束了~~