如何将日期“MM-dd-yyyy hh:mm:ss a”转换为文字格式?
我想创建一个将日期转换成文本格式,如“刚才,2分钟,1小时,1天,10月10日”的功能。这里就是我在得到一个错误,我的示例代码:如何将日期“MM-dd-yyyy hh:mm:ss a”转换为文字格式?
let components = cal.components([.Day,.Hour,.Minute], fromDate: date, toDate: NSDate(), options:[])
这里是我的全码:
func getTextToDisplayFormattingDate(date: NSDate) -> String {
var textToDisplay = ""
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy hh:mm:ss a"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
let cal = NSCalendar.current
let components = cal.components([.Day,.Hour,.Minute], fromDate: date, toDate: NSDate(), options:[])
switch components.day {
case 0:
if components.hour == 0 {
if components.minute <= 0 {
textToDisplay = "just now "
} else {
textToDisplay = "\(components.minute) min"
}
} else {
textToDisplay = "\(components.hours) hrs"
}
case 1...6:
textToDisplay = "\(components.day) d"
default:
dateFormatter.dateFormat = "MMM dd"
textToDisplay = dateFormatter.string(from: date as Date)
}
return textToDisplay
}
其实,这是难以计算的日,时,分日期由NSCalendar.components
2之间,你的程序会导致意想不到的结果。通常,我们通过它们之间的TimeInterval
进行计算。在SWIFT 3.2或SWIFT 4.0试试这个:
func getTextToDisplayFormattingDate(date: Date) -> String {
var textToDisplay = ""
let now = Date()
let timeInterval = now.timeIntervalSince(date)
if timeInterval < 60 {
textToDisplay = "just now "
}
else if timeInterval < 60 * 60 {
textToDisplay = "\(Int(timeInterval/60)) min"
}
else if timeInterval < 60 * 60 * 24 {
textToDisplay = "\(Int(timeInterval/60/60)) hrs"
}
else {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "UTC")
if timeInterval < 60 * 60 * 24 * 6 { //less than 6 days
// For getting days
// textToDisplay = "\(Int(timeInterval/60/60/24)) d"
// For getting weekday name
dateFormatter.dateFormat = "EEEE"
textToDisplay = dateFormatter.string(from: date) //weekday name
}
else{
dateFormatter.dateFormat = "MMM dd"
textToDisplay = dateFormatter.string(from: date as Date)
}
}
return textToDisplay
}
记住使用常量来替换表示喜欢60 * 60 * 24
以提高性能。
如果不到6天,我该如何显示周日名称。例如:刚才小时,周四,周三,周二,oct'10,oct'9 @yun陈 –
@WhenKey,我更新的,如果不超过6日获得工作日名称的答案。尝试一下。 –
使用这样的:
的变化在我的代码:
- 在地方
NSDate
,我用Date
。 - 在地方
NSCalendar
我用Calendar
-
components(_:from:to:options:)
已更名为dateComponents(_:from:to:)
-
components
值一样day, minute, hour
是optional
。这就是为什么我添加检查之前切换。func getTextToDisplayFormattingDate(date: Date) -> String { var textToDisplay = "" let dateFormatter = DateFormatter() dateFormatter.dateFormat = "MM-dd-yyyy hh:mm:ss a" dateFormatter.timeZone = TimeZone(identifier: "UTC") let cal = Calendar.current let components = cal.dateComponents([.day, .hour , .minute], from: date, to: Date()) if let day = components.day, let minute = components.minute, let hour = components.hour { switch day { case 0: if hour == 0 { if minute <= 0 { textToDisplay = "just now " } else { textToDisplay = "\(minute) min" } } else { textToDisplay = "\(hour) hrs" } case 1...6: textToDisplay = "\(day) d" default: dateFormatter.dateFormat = "MMM dd" textToDisplay = dateFormatter.string(from: date) } } return textToDisplay }
从你的斯威夫特3码摆脱所有的斯威夫特2语法的。 – rmaddy