几分钟秒秒秒
我想在iOS中将时间小时分秒转换为秒。 有没有在这方面build立的方法? 我怎样才能做到这一点?
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *componentsDiff = [gregorianCalendar components:NSHourCalendarUnit fromDate:[NSDate date]];
下面的代码插入date作为一个string,然后返回第二个数字。
- (NSNumber *)dateToSecondConvert:(NSString *)string { NSArray *components = [string componentsSeparatedByString:@":"]; NSInteger hours = [[components objectAtIndex:0] integerValue]; NSInteger minutes = [[components objectAtIndex:1] integerValue]; NSInteger seconds = [[components objectAtIndex:2] integerValue]; return [NSNumber numberWithInteger:(hours * 60 * 60) + (minutes * 60) + seconds]; }
愿这帮助很多。
大量的例子,但是你可以这样做:
NSDate *now = [NSDate date]; NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comp = [calendar components:NSHourCalendarUnit fromDate:now]; //[comp hour] //[comp minute] //[comp second]
也许我误解了这个问题,但是从1970年起,这会给你现在的时间
[[NSDate date] timeIntervalSince1970]