如何用文字表达现在的时间
How to express current time in words
我可以得到当前时间,现在我想用文字输出。
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)
print("hours = \(hour):\(minutes):\(seconds)")
输出
10:30
如何得到这样的 -
十点半
此代码将向您展示实现最终目标的可能性。请记住,如上所述,您需要为您想要实现的组合创建逻辑。
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)")
}
正如@MwcsMac 在他的回答中指出的那样,解决这个问题的关键是 Formatter
(曾经称为 NSFormatter
),特别是将 .numberStyle
设置为 .spellOut
.
虽然这会选择当前的语言环境(以及语言),但问题是除英语之外的许多其他语言不使用相同的 "half-past"、"quarter-to" 术语 - 例如,在德语 10:30 是 "halb elf",字面意思是 "half (to) eleven".
编写假定语言环境为English/American的代码是非常糟糕的做法,如果出现以下情况,应用可能会被拒绝它在这些区域之外提供,所以最好的办法是将“10:30”格式设置为 "ten thirty"、"zehn dreißig".
向@MwcsMac 道歉的代码:
import Foundation
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minute = calendar.component(.minute, from: date)
func spell(_ number: Int, _ localeID: String) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
// Specify the locale or you will inherit the current default locale
formatter.locale = Locale(identifier: localeID)
if let s = formatter.string(from: NSNumber(value: number)) {
// AVOID forced unwrapping at all times!
return s
} else {
return "<Invalid>" // or make return optional and return `nil`
}
}
spell(hour, "EN") + " " + spell(minute, "EN") // "nineteen thirty-three"
spell(hour, "FR") + " " + spell(minute, "FR") // ""dix-neuf trente-trois"
spell(hour, "AR") + " " + spell(minute, "AR") // "تسعة عشر ثلاثة و ثلاثون"
我可以得到当前时间,现在我想用文字输出。
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)
print("hours = \(hour):\(minutes):\(seconds)")
输出
10:30
如何得到这样的 -
十点半
此代码将向您展示实现最终目标的可能性。请记住,如上所述,您需要为您想要实现的组合创建逻辑。
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)")
}
正如@MwcsMac 在他的回答中指出的那样,解决这个问题的关键是 Formatter
(曾经称为 NSFormatter
),特别是将 .numberStyle
设置为 .spellOut
.
虽然这会选择当前的语言环境(以及语言),但问题是除英语之外的许多其他语言不使用相同的 "half-past"、"quarter-to" 术语 - 例如,在德语 10:30 是 "halb elf",字面意思是 "half (to) eleven".
编写假定语言环境为English/American的代码是非常糟糕的做法,如果出现以下情况,应用可能会被拒绝它在这些区域之外提供,所以最好的办法是将“10:30”格式设置为 "ten thirty"、"zehn dreißig".
向@MwcsMac 道歉的代码:
import Foundation
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minute = calendar.component(.minute, from: date)
func spell(_ number: Int, _ localeID: String) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
// Specify the locale or you will inherit the current default locale
formatter.locale = Locale(identifier: localeID)
if let s = formatter.string(from: NSNumber(value: number)) {
// AVOID forced unwrapping at all times!
return s
} else {
return "<Invalid>" // or make return optional and return `nil`
}
}
spell(hour, "EN") + " " + spell(minute, "EN") // "nineteen thirty-three"
spell(hour, "FR") + " " + spell(minute, "FR") // ""dix-neuf trente-trois"
spell(hour, "AR") + " " + spell(minute, "AR") // "تسعة عشر ثلاثة و ثلاثون"