如何转换持续时间formsyoutube API在迅速?

我是新的快速开发人员,我不知道如何将时间从YouTube的API时间转换为正常的时间格式?

只考虑以hh:mm:ss格式返回值的简单实现。

extension String { func getYoutubeFormattedDuration() -> String { let formattedDuration = self.stringByReplacingOccurrencesOfString("PT", withString: "").stringByReplacingOccurrencesOfString("H", withString: ":").stringByReplacingOccurrencesOfString("M", withString: ":").stringByReplacingOccurrencesOfString("S", withString: "") let components = formattedDuration.componentsSeparatedByString(":") var duration = "" for component in components { duration = duration.characters.count > 0 ? duration + ":" : duration if component.characters.count < 2 { duration += "0" + component continue } duration += component } return duration } } 

** Swift 3

  func getYoutubeFormattedDuration() -> String { let formattedDuration = self.replacingOccurrences(of: "PT", with: "").replacingOccurrences(of: "H", with:":").replacingOccurrences(of: "M", with: ":").replacingOccurrences(of: "S", with: "") let components = formattedDuration.components(separatedBy: ":") var duration = "" for component in components { duration = duration.characters.count > 0 ? duration + ":" : duration if component.characters.count < 2 { duration += "0" + component continue } duration += component } return duration } 

样品结果:

 "PT3H2M31S".getYoutubeFormattedDuration() //returns "03:02:31" "PT2M31S".getYoutubeFormattedDuration() //returns "02:31" "PT31S".getYoutubeFormattedDuration() //returns "31" 

使用string扩展:

 extension String{ func formatDurationsFromYoutubeAPItoNormalTime (targetString : String) ->String{ var timeDuration : NSString! let string: NSString = targetString if string.rangeOfString("H").location == NSNotFound && string.rangeOfString("M").location == NSNotFound{ if string.rangeOfString("S").location == NSNotFound { timeDuration = NSString(format: "00:00") } else { var secs: NSString = targetString secs = secs.substringFromIndex(secs.rangeOfString("PT").location + "PT".characters.count) secs = secs.substringToIndex(secs.rangeOfString("S").location) timeDuration = NSString(format: "00:%02d", secs.integerValue) } } else if string.rangeOfString("H").location == NSNotFound { var mins: NSString = targetString mins = mins.substringFromIndex(mins.rangeOfString("PT").location + "PT".characters.count) mins = mins.substringToIndex(mins.rangeOfString("M").location) if string.rangeOfString("S").location == NSNotFound { timeDuration = NSString(format: "%02d:00", mins.integerValue) } else { var secs: NSString = targetString secs = secs.substringFromIndex(secs.rangeOfString("M").location + "M".characters.count) secs = secs.substringToIndex(secs.rangeOfString("S").location) timeDuration = NSString(format: "%02d:%02d", mins.integerValue, secs.integerValue) } } else { var hours: NSString = targetString hours = hours.substringFromIndex(hours.rangeOfString("PT").location + "PT".characters.count) hours = hours.substringToIndex(hours.rangeOfString("H").location) if string.rangeOfString("M").location == NSNotFound && string.rangeOfString("S").location == NSNotFound { timeDuration = NSString(format: "%02d:00:00", hours.integerValue) } else if string.rangeOfString("M").location == NSNotFound { var secs: NSString = targetString secs = secs.substringFromIndex(secs.rangeOfString("H").location + "H".characters.count) secs = secs.substringToIndex(secs.rangeOfString("S").location) timeDuration = NSString(format: "%02d:00:%02d", hours.integerValue, secs.integerValue) } else if string.rangeOfString("S").location == NSNotFound { var mins: NSString = targetString mins = mins.substringFromIndex(mins.rangeOfString("H").location + "H".characters.count) mins = mins.substringToIndex(mins.rangeOfString("M").location) timeDuration = NSString(format: "%02d:%02d:00", hours.integerValue, mins.integerValue) } else { var secs: NSString = targetString secs = secs.substringFromIndex(secs.rangeOfString("M").location + "M".characters.count) secs = secs.substringToIndex(secs.rangeOfString("S").location) var mins: NSString = targetString mins = mins.substringFromIndex(mins.rangeOfString("H").location + "H".characters.count) mins = mins.substringToIndex(mins.rangeOfString("M").location) timeDuration = NSString(format: "%02d:%02d:%02d", hours.integerValue, mins.integerValue, secs.integerValue) } } return timeDuration as String } } 

用法:

 override func viewWillAppear(animated: Bool) { let youtubeVideoDurationString = "PT15M51S" let resultString = youtubeVideoDurationString.formatDurationsFromYoutubeAPItoNormalTime(youtubeVideoDurationString) print(resultString) } 

转换小时:分钟:第二格式 : –

使用string扩展:

 extension String{ func parseVideoDurationOfYoutubeAPI(videoDuration: String?) -> String { var videoDurationString = videoDuration! as NSString var hours: Int = 0 var minutes: Int = 0 var seconds: Int = 0 let timeRange = videoDurationString.rangeOfString("T") videoDurationString = videoDurationString.substringFromIndex(timeRange.location) while videoDurationString.length > 1 { videoDurationString = videoDurationString.substringFromIndex(1) let scanner = NSScanner(string: videoDurationString as String) as NSScanner var part: NSString? scanner.scanCharactersFromSet(NSCharacterSet.decimalDigitCharacterSet(), intoString: &part) let partRange: NSRange = videoDurationString.rangeOfString(part! as String) videoDurationString = videoDurationString.substringFromIndex(partRange.location + partRange.length) let timeUnit: String = videoDurationString.substringToIndex(1) if (timeUnit == "H") { hours = Int(part as! String)! } else if (timeUnit == "M") { minutes = Int(part as! String)! } else if (timeUnit == "S") { seconds = Int(part! as String)! } else{ } } return String(format: "%02d:%02d:%02d", hours, minutes, seconds) 

}}

用法:override func viewWillAppear(animated:Bool){

  let youtubeVideoDurationString = "PT15M51S" let result_Hour_Minute_Second = youtubeVideoDurationString.parseVideoDurationOfYoutubeAPI("PT15M51S") as String print("result_Hour_Minute_Second: \(result_Hour_Minute_Second)") }