本文介绍了小时分钟秒到秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ios中将时间小时分钟转换为秒。
这是否有内置方法?
我该怎么办?

I want to convert time hours minutes seconds to seconds in ios.Is there any in built method for this?How can I do this?

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *componentsDiff = [gregorianCalendar components:NSHourCalendarUnit fromDate:[NSDate date]];


推荐答案

将插入日期的代码作为字符串,然后返回第二个数字。

Following code for the insert date as a string and then return numbers of second.

- (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];
}

这可以帮助很多。

这篇关于小时分钟秒到秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:04
查看更多