我想在ios中将时间小时分钟秒转换为秒。
有没有内置的方法?
我怎样才能做到这一点?
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsDiff = [gregorianCalendar components:NSHourCalendarUnit fromDate:[NSDate date]];
以下代码将日期作为字符串插入,然后返回秒数。
- (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];
}
愿这对很多人有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)