如何将日期"MM-dd-yyyy hh:mm:ss a"转换成文本格式?

How to convert date "MM-dd-yyyy hh:mm:ss a" into text format?

我正在尝试创建一个将日期转换为文本格式的函数,如 "Just now, 2mins, 1hour, 1day, oct 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
}

这样使用:

我的代码更改:

  1. 代替NSDate,我使用了Date
  2. 代替NSCalendar我用了Calendar
  3. components(_:from:to:options:) 已重命名为 dateComponents(_:from:to:)
  4. componentsday, 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
    }
    

实际上,很难计算 NSCalendar.components 两个日期之间的 天、小时、分钟 ,您的程序会导致意外结果。通常,我们通过它们之间的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 以提高性能。