如何用文字表达当前时间

时间:2017-02-20 17:10:56

标签: swift foundation

我能够获得当前时间,现在我想用文字输出它。

ngOnInit() {
   this.getPropertyStatus("whatever name");
}

private getPropertyStatus(name: string): string {
  return this.doctor ? this.doctor.getPropertyStatus(name) : '';
}

输出

10:30

如何做到这一点 -

现在是十点半了

1 个答案:

答案 0 :(得分:3)

此代码将向您显示可以实现最终目标。请记住,如上所述,您需要为要实现的组合创建逻辑。

Swift 3

let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)

func spell(_ number: Int) -> String {
    let formatter = NumberFormatter()
    formatter.numberStyle = .spellOut // This will convert the number to words.
    return formatter.string(from: NSNumber(value: number))!
}

if minutes == 30 {
    let hourString = spell(hour)
    print("It's half past \(hourString)")
} 

这可能是一种在时间范围内使用范围的方法。

if case 1 ... 14 = minutes {
     let hourString = spell(hour) // This will give your hour in word form
     let minString = spell(minutes) // This will give your minutes in word form
     print("It's \(minString) past \(hourString)")
}