我有一个(有点?)关于Swift中的时间转换的基本问题。

我有一个整数,我想转换成小时/分钟/秒。

示例:Int = 27005会给我:

7 Hours  30 Minutes 5 Seconds

我知道如何在PHP中做到这一点,但是,唉,swift不是PHP。


当前回答

基于Vadian的回答,我写了一个扩展,它接受Double(其中TimeInterval是类型别名),并输出一个格式化为时间的字符串。

extension Double {
  func asString(style: DateComponentsFormatter.UnitsStyle) -> String {
    let formatter = DateComponentsFormatter()
    formatter.allowedUnits = [.hour, .minute, .second, .nanosecond]
    formatter.unitsStyle = style
    return formatter.string(from: self) ?? ""
  }
}

这里是各种DateComponentsFormatter。UnitsStyle选项看起来像这样:

10000.asString(style: .positional)  // 2:46:40
10000.asString(style: .abbreviated) // 2h 46m 40s
10000.asString(style: .short)       // 2 hr, 46 min, 40 sec
10000.asString(style: .full)        // 2 hours, 46 minutes, 40 seconds
10000.asString(style: .spellOut)    // two hours, forty-six minutes, forty seconds
10000.asString(style: .brief)       // 2hr 46min 40sec

其他回答

将数字转换为字符串形式的时间

func convertToHMS(number: Int) -> String {
  let hour    = number / 3600;
  let minute  = (number % 3600) / 60;
  let second = (number % 3600) % 60 ;
  
  var h = String(hour);
  var m = String(minute);
  var s = String(second);
  
  if h.count == 1{
      h = "0\(hour)";
  }
  if m.count == 1{
      m = "0\(minute)";
  }
  if s.count == 1{
      s = "0\(second)";
  }
  
  return "\(h):\(m):\(s)"
}
print(convertToHMS(number:3900))

恕我直言,最简单的方法是:

let hours = time / 3600
let minutes = (time / 60) % 60
let seconds = time % 60
return String(format: "%0.2d:%0.2d:%0.2d", hours, minutes, seconds)

Neek的答案不正确。

这是正确的版本

func seconds2Timestamp(intSeconds:Int)->String {
   let mins:Int = (intSeconds/60)%60
   let hours:Int = intSeconds/3600
   let secs:Int = intSeconds%60

   let strTimestamp:String = ((hours<10) ? "0" : "") + String(hours) + ":" + ((mins<10) ? "0" : "") + String(mins) + ":" + ((secs<10) ? "0" : "") + String(secs)
   return strTimestamp
}

定义

func secondsToHoursMinutesSeconds(_ seconds: Int) -> (Int, Int, Int) {
    return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}

Use

> secondsToHoursMinutesSeconds(27005)
(7,30,5)

or

let (h,m,s) = secondsToHoursMinutesSeconds(27005)

上面的函数使用Swift元组一次返回三个值。使用let (var,…)语法解构元组,如果需要,也可以访问单个元组成员。

如果你真的需要用Hours等文字打印出来,那么可以使用这样的东西:

func printSecondsToHoursMinutesSeconds(_ seconds: Int) {
  let (h, m, s) = secondsToHoursMinutesSeconds(seconds)
  print ("\(h) Hours, \(m) Minutes, \(s) Seconds")
}

注意,上面的secondstohoursminutessecseconds()实现适用于Int参数。如果你想要一个Double版本,你需要决定返回值是什么-可能是(Int, Int, Double)或可能是(Double, Double, Double)。你可以尝试这样做:

func secondsToHoursMinutesSeconds(seconds: Double) -> (Double, Double, Double) {
  let (hr,  minf) = modf(seconds / 3600)
  let (min, secf) = modf(60 * minf)
  return (hr, min, 60 * secf)
}

Xcode 12.1。Swift 5

DateComponentsFormatter:创建字符串表示的格式化程序, 通过使用unitsStyle,你可以得到一个你想要的字符串,并提到allowedUnits。 例如:output for unitsStyle:: for 10000秒

满=“2小时46分49秒” 位置= "2:46:40" 缩写为"2h 46m 40s" 拼写= " 2小时46分40秒" 短=“2小时46分40秒” 简短= "2小时46分40秒"

使用方便:

 let time = convertSecondsToHrMinuteSec(seconds: 10000)


func convertSecondsToHrMinuteSec(seconds:Int) -> String{
     let formatter = DateComponentsFormatter()
     formatter.allowedUnits = [.hour, .minute, .second]
     formatter.unitsStyle = .full
    
     let formattedString = formatter.string(from:TimeInterval(seconds))!
     print(formattedString)
     return formattedString
    }