嗨我正在獲取作爲一個字符串的時間值。我得到的數字是在幾秒鐘內。現在我想通過使用swift3將秒數轉換爲分鐘。如何將Swift3中的字符串以秒爲單位更改爲分鐘?
我得到的秒數是: 540這是在幾秒鐘內。
現在我想將秒轉換爲分鐘。 例如它應該顯示爲09:00。
如何使用Swift3代碼實現此目的。 目前我沒有使用任何轉換代碼。
let duration: TimeInterval = 7200.0
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale
formatter.allowedUnits = [ .hour, .minute, .second ] // Units to display in the formatted string
formatter.zeroFormattingBehavior = [ .pad ] // Pad with zeroes where appropriate for the locale
let formattedDuration = formatter.string(from: duration)
看到這個答案:https://stackoverflow.com/a/46667805/6257435 – DonMag
@DonMag它不會將我的時間轉換爲09:00 –
對不起,你可以用它作爲起點......前三行將你的持續時間分爲小時,分鐘和秒......從這裏開始,應該很簡單字符串根據需要。 – DonMag