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

时间:2017-10-15 01:40:36

标签: swift nsdate

我正在尝试创建一个将日期转换为文本格式的函数,例如"刚才,2分钟,1小时,1天,10月10日和34日。这是我的示例代码,我在以下位置收到错误:

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
}

error

2 个答案:

答案 0 :(得分:2)

像这样使用:

我的代码更改:

  1. 我使用NSDate取代Date代替NSCalendar
  2. 取代Calendar我使用components(_:from:to:options:)
  3. dateComponents(_:from:to:)已重命名为components
  4. day, minute, hour之类的值optionalfunc 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 } 。这就是为什么我在切换之前添加了检查。

     select to_char(to_date(InvoiceDate,'DD-MON-YY'), 'YYYY-MM-DD') from CHINOOK.invoice;
    

答案 1 :(得分:0)

实际上,很难按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之类的表达式以提高性能。