答案 0 :(得分:5)
NSDate *today = [NSDate date];
给你当前的时间和日期。
答案 1 :(得分:1)
NSDate *today = [NSDate date];
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:today];
[calendar release];
NSInteger hour = [dateComponents hour];
NSInteger min = [dateComponents minute];
NSInteger sec = [dateComponents second];
这实际上只是从日期开始,而不是整个日期。它以单独的整数形式获得时间,hour
,min
,sec
。要将它们全部放回一个字符串中,您可以执行以下操作:
NSString *time = [NSString stringWithFormat:@"%i:%i:%i", hour, min, sec];