我是新swift開發者,我不知道如何將持續時間從YouTube的API轉換爲正常時間格式?如何在swift中轉換持續時間形式的youtube api?
1
A
回答
1
使用字符串擴展:
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)
}
0
轉換小時:分鐘:秒格式: -
使用字符串擴展:
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)
} }
用法: 倍率FUNC viewWillAppear中(動畫:BOOL){
let youtubeVideoDurationString = "PT15M51S"
let result_Hour_Minute_Second = youtubeVideoDurationString.parseVideoDurationOfYoutubeAPI("PT15M51S") as String
print("result_Hour_Minute_Second: \(result_Hour_Minute_Second)")
}
2
更簡單的實現考慮到在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
}
}
**斯威夫特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"
相關問題
- 1. 如何轉換Java中的Youtube API V3持續時間
- 2. 如何將YouTube API持續時間(格式爲PT#M#S的ISO 8601持續時間)轉換爲秒
- 3. Youtube API視頻持續時間格式
- 4. 轉換YouTube API持續時間(格式爲ISO 8601持續時間),以H:M:S,時制,沒有 「T」 在JavaScript
- 5. 如何轉換YouTube視頻持續時間從ISO8601
- 6. 轉換MP3持續時間
- 7. 將Youtube Data API V3視頻持續時間格式轉換爲PHP中的標準時間
- 8. 如何在ImageSwitcher中控制轉換的持續時間Android
- 9. 如何使用simpeXML從youtube API獲取視頻持續時間?
- 10. Youtube API v3,如何獲得視頻持續時間?
- 11. Ruby on Rails - YouTube API - 如何格式化持續時間ISO 8601(PT45S)
- 12. 將ISO 8601時間格式轉換爲正常持續時間
- 13. 如何將持續時間從ISO格式PT1h30M轉換爲基於php分鐘數的int持續時間?
- 14. PHP:如何串持續時間轉換爲ISO 8601持續時間格式? (即「30分鐘」到「PT30M」)
- 15. 將NSTimeInterval持續時間轉換爲NSString
- 16. UIPageViewController轉換速度/持續時間?
- 17. 將秒轉換爲持續時間
- 18. 在willAnimateFirstHalfOfRotationToInterfaceOrientation中獲取旋轉視圖的矩形:持續時間:
- 19. 如何在控制中心製作持續時間滑塊? Swift
- 20. YouTube GData API - 查詢具有特定持續時間的視頻
- 21. 從YouTube獲取持續時間url
- 22. 如何將UTC時間轉換爲Unix時間戳在swift中?
- 23. 每個轉換屬性的不同轉換持續時間
- 24. Youtube Api v3多個視頻持續時間
- 25. Youtube搜索API v3-按持續時間篩選
- 26. 如何提取只包含持續時間的持續時間?
- 27. 使用avconv和youtube-dl從flv轉換爲mp3後的錯誤持續時間
- 28. ASP NET形式授權:如何減少持續時間?
- 29. JS - Youtube Iframe API返回錯誤的當前時間和持續時間?
- 30. 用於持續時間計算的時間戳快速轉換
@please幫我 – sony
這裏是你的答案,http://stackoverflow.com/a/16254918/2713079 – itsji10dra
這個答案從正常的日期轉換爲iOS 但是我想從iOS轉換爲常規? – sony