开发者

Convert HH:MM:SS to seconds in xcode

Hi I have the code to separate hour,min,sec Now i have to convert it in to 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);


If you want to find out how many seconds the hours, minutes and seconds total, you can do something like this:

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


Just an alternative if you have to handle both "HH:mm:ss" and "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
    }
}


Swift 4 - improved from @Beslan Tularov's answer.

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)
    }
}

Usage

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


You can also try like this:

extension String{
    //format hh:mm:ss or hh:mm or HH:mm
    var secondFromString : Int{
        var n = 3600
        return self.components(separatedBy: ":").reduce(0) {
            defer { n /= 60 }
            return $0 + (Int($1) ?? 0) * n
        }
    }
}

var result = "00:44:22".secondFromString //2662
result = "00:44".secondFromString //2640
result = "01:44".secondFromString //6240
result = "999:10".secondFromString //3597000
result = "02:44".secondFromString //9840
result = "00:01".secondFromString //60
result = "00:01:01".secondFromString //61
result = "01:01:01".secondFromString //3661
result = "12".secondFromString //43200
result = "abcd".secondFromString //0


From what you've,

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


The following is a String extension for converting a time string (HH:mm:ss) to seconds

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)
    }
}

how to use

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


You can use the following extension to do that (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
   }
}

And for example, use it like:

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

Which will print:

3790 seconds
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜