如何在 Swift3 中以秒为单位将字符串更改为分钟?
How to change the string in seconds to minutes in 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)
这是一种方法:
let duration: TimeInterval = 540
// new Date object of "now"
let date = Date()
// create Calendar object
let cal = Calendar(identifier: .gregorian)
// get 12 O'Clock am
let start = cal.startOfDay(for: date)
// add your duration
let newDate = start.addingTimeInterval(duration)
// create a DateFormatter
let formatter = DateFormatter()
// set the format to minutes:seconds (leading zero-padded)
formatter.dateFormat = "mm:ss"
let resultString = formatter.string(from: newDate)
// resultString is now "09:00"
// if you want hours
// set the format to hours:minutes:seconds (leading zero-padded)
formatter.dateFormat = "HH:mm:ss"
let resultString = formatter.string(from: newDate)
// resultString is now "00:09:00"
如果您希望将以秒为单位的持续时间格式化为 "time of day,",请将格式字符串更改为:
formatter.dateFormat = "hh:mm:ss a"
现在,生成的字符串应该是:
"12:09:00 AM"
当然,这会因地区而异。
你可以使用这个:
func timeFormatter(_ seconds: Int32) -> String! {
let h: Float32 = Float32(seconds / 3600)
let m: Float32 = Float32((seconds % 3600) / 60)
let s: Float32 = Float32(seconds % 60)
var time = ""
if h < 10 {
time = time + "0" + String(Int(h)) + ":"
} else {
time = time + String(Int(h)) + ":"
}
if m < 10 {
time = time + "0" + String(Int(m)) + ":"
} else {
time = time + String(Int(m)) + ":"
}
if s < 10 {
time = time + "0" + String(Int(s))
} else {
time = time + String(Int(s))
}
return time
}
考虑使用 Swift Moment 框架:https://github.com/akosma/SwiftMoment
let duration: TimeInterval = 7200.0
let moment = Moment(duration)
let formattedDuration = "\(moment.minutes):\(moment.seconds)"
您好,我正在获取字符串形式的时间值。我得到的数字以秒为单位。现在我想使用 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)
这是一种方法:
let duration: TimeInterval = 540
// new Date object of "now"
let date = Date()
// create Calendar object
let cal = Calendar(identifier: .gregorian)
// get 12 O'Clock am
let start = cal.startOfDay(for: date)
// add your duration
let newDate = start.addingTimeInterval(duration)
// create a DateFormatter
let formatter = DateFormatter()
// set the format to minutes:seconds (leading zero-padded)
formatter.dateFormat = "mm:ss"
let resultString = formatter.string(from: newDate)
// resultString is now "09:00"
// if you want hours
// set the format to hours:minutes:seconds (leading zero-padded)
formatter.dateFormat = "HH:mm:ss"
let resultString = formatter.string(from: newDate)
// resultString is now "00:09:00"
如果您希望将以秒为单位的持续时间格式化为 "time of day,",请将格式字符串更改为:
formatter.dateFormat = "hh:mm:ss a"
现在,生成的字符串应该是:
"12:09:00 AM"
当然,这会因地区而异。
你可以使用这个:
func timeFormatter(_ seconds: Int32) -> String! {
let h: Float32 = Float32(seconds / 3600)
let m: Float32 = Float32((seconds % 3600) / 60)
let s: Float32 = Float32(seconds % 60)
var time = ""
if h < 10 {
time = time + "0" + String(Int(h)) + ":"
} else {
time = time + String(Int(h)) + ":"
}
if m < 10 {
time = time + "0" + String(Int(m)) + ":"
} else {
time = time + String(Int(m)) + ":"
}
if s < 10 {
time = time + "0" + String(Int(s))
} else {
time = time + String(Int(s))
}
return time
}
考虑使用 Swift Moment 框架:https://github.com/akosma/SwiftMoment
let duration: TimeInterval = 7200.0
let moment = Moment(duration)
let formattedDuration = "\(moment.minutes):\(moment.seconds)"