Swift “如何转换日期”;MM dd yyyy hh:MM:ss a“;转换成文本格式?

Swift “如何转换日期”;MM dd yyyy hh:MM:ss a“;转换成文本格式?,swift,nsdate,Swift,Nsdate,我正在尝试创建一个函数,将日期转换为文本格式,如“刚才,2分钟,1小时,1天,10月10日”。这是我的示例代码,我在其中得到一个错误: let components = cal.components([.Day,.Hour,.Minute], fromDate: date, toDate: NSDate(), options:[]) 这是我的全部代码: func getTextToDisplayFormattingDate(date: NSDate) -> String { va

我正在尝试创建一个函数,将日期转换为文本格式,如“刚才,2分钟,1小时,1天,10月10日”。这是我的示例代码,我在其中得到一个错误:

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
}
func getTextToDisplayFormattingDate(日期:NSDate)->字符串{
var textToDisplay=“”
让dateFormatter=dateFormatter()
dateFormatter.dateFormat=“MM dd yyyy hh:MM:ss a”
dateFormatter.timeZone=时区(标识符:“UTC”)
设cal=NSCalendar.current
让components=cal.components([.Day、.Hour、.Minute],fromDate:date,toDate:NSDate(),选项:[])
开关组件。每天{
案例0:
如果components.hour==0{
如果components.minute这样使用:

我的代码中的更改:

  • 我用
    Date
    代替了
    NSDate
  • 我用日历代替了日历
  • 组件(从:到:选项:)
    已重命名为
    日期组件(从:到:)
  • 组件
    值,如
    天、分钟、小时
    可选的
    。这就是我在切换前添加检查的原因

    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
    }
    
    func getTextToDisplayFormattingDate(日期:date)->String{
    var textToDisplay=“”
    让dateFormatter=dateFormatter()
    dateFormatter.dateFormat=“MM dd yyyy hh:MM:ss a”
    dateFormatter.timeZone=时区(标识符:“UTC”)
    设cal=Calendar.current
    让components=cal.dateComponents([.day、.hour、.minute],from:date,to:date())
    如果let day=components.day,let minute=components.minute,let hour=components.hour{
    转换日{
    案例0:
    如果小时==0{
    
    如果minute实际上,很难通过
    NSCalendar.components
    计算两个日期之间的天、小时、分钟,您的过程将导致意外结果。通常,我们通过它们之间的
    时间间隔
    进行计算。请在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
    }
    
    func getTextToDisplayFormattingDate(日期:date)->String{
    var textToDisplay=“”
    让我们现在=日期()
    让timeInterval=now.timeIntervalSince(日期)
    如果时间间隔<60{
    textToDisplay=“刚才”
    }
    否则,如果时间间隔<60*60{
    textToDisplay=“\(Int(时间间隔/60))分钟”
    }
    否则,如果时间间隔<60*60*24{
    textToDisplay=“\(Int(时间间隔/60/60))小时”
    }
    否则{
    让dateFormatter=dateFormatter()
    dateFormatter.timeZone=时区(标识符:“UTC”)
    如果时间间隔<60*60*24*6{//小于6天
    //为了得到几天
    //textToDisplay=“\(Int(timeInterval/60/60/24))d”
    //用于获取工作日名称
    dateFormatter.dateFormat=“EEEE”
    textToDisplay=dateFormatter.string(from:date)//工作日名称
    }
    否则{
    dateFormatter.dateFormat=“MMM dd”
    textToDisplay=dateFormatter.string(从:日期为日期)
    }
    }
    返回文本显示
    }
    

    请记住使用常量替换表达式,如
    60*60*24
    ,以提高性能。

    从Swift 3代码中删除所有Swift 2语法。您不需要将
    日期转换为日期,它已经是一个
    日期
    。考虑到问题的性质,如果您在问题中添加说明,可能会有所帮助回答:指出您需要进行的所有修复及其原因。@ViniApp非常感谢!!它真的非常有效。感谢如果不足6天,我如何显示工作日名称。例如:现在,小时,周四,周三,周二,10月10日,10月9日@yunchen@WhenKey,如果少于6天,我更新了获取工作日名称的答案。请尝试。