将HH:MM:SS转换为xcode中的秒数

时间:2011-06-22 08:17:00

标签: ios objective-c

您好我有代码分开小时,分钟,秒 现在我必须将其转换为seconds.and nsnumber

    NSRange range = [string rangeOfString:@":"];
    NSString *hour = [string substringToIndex:range.location];
    NSLog(@"time %@",hour);

    NSRange range1= NSMakeRange(2,2);
    NSString *min = [string substringWithRange:range1];
    NSLog(@"time %@",min);
    NSRange range2 = NSMakeRange(5,2);
    NSString *sec = [string substringWithRange:range2];
    NSLog(@"time %@",sec);

6 个答案:

答案 0 :(得分:13)

如果你想知道小时,分钟和秒总数的秒数,你可以这样做:

- (NSNumber *)secondsForTimeString:(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];
}

答案 1 :(得分:1)

Swift 4 - 改进了@Beslan Tularov的回答。

extension String{

    var integer: Int {
        return Int(self) ?? 0
    }

    var secondFromString : Int{
        var components: Array = self.components(separatedBy: ":")
        let hours = components[0].integer
        let minutes = components[1].integer
        let seconds = components[2].integer
        return Int((hours * 60 * 60) + (minutes * 60) + seconds)
    }
}

用法

let xyz = "00:44:22".secondFromString
//result : 2662

答案 2 :(得分:0)

从你的角度来看,

double totalSeconds = [hour doubleValue] * 60 * 60 + [min doubleValue] * 60 + [sec doubleValue];
NSNumber * seconds = [NSNumber numberWithDouble:totalSeconds];

答案 3 :(得分:0)

以下是用于将时间字符串(HH:mm:ss)转换为秒的字符串扩展

extension String {
    func secondsFromString (string: String) -> Int {
        var components: Array = string.componentsSeparatedByString(":")
        var hours = components[0].toInt()!
        var minutes = components[1].toInt()!
        var seconds = components[2].toInt()!
        return Int((hours * 60 * 60) + (minutes * 60) + seconds)
    }
}
  

如何使用

var exampleString = String().secondsFromString("00:30:00")

答案 4 :(得分:0)

您可以使用以下扩展程序( Swift 3 ):

extension String {
    func numberOfSeconds() -> Int {
       var components: Array = self.components(separatedBy: ":")
       let hours = Int(components[0]) ?? 0
       let minutes = Int(components[1]) ?? 0
       let seconds = Int(components[2]) ?? 0
       return (hours * 3600) + (minutes * 60) + seconds
   }
}

例如,使用它像:

let seconds = "01:30:10".numberOfSeconds()
print("%@ seconds", seconds)

将打印:

3790 seconds

答案 5 :(得分:0)

如果您必须同时处理“ HH:mm:ss”和“ mm:ss”

    extension String {
    /**
     Converts a string of format HH:mm:ss into seconds
     ### Expected string format ###
     ````
        HH:mm:ss or mm:ss
     ````
     ### Usage ###
     ````
        let string = "1:10:02"
        let seconds = string.inSeconds  // Output: 4202
     ````
     - Returns: Seconds in Int or if conversion is impossible, 0
     */
    var inSeconds : Int {
        var total = 0
        let secondRatio = [1, 60, 3600]    // ss:mm:HH
        for (i, item) in self.components(separatedBy: ":").reversed().enumerated() {
            if i >= secondRatio.count { break }
            total = total + (Int(item) ?? 0) * secondRatio[i]
        }
        return total
    }
}