NSDateFormatter。在swift

时间:2016-06-13 15:15:10

标签: swift time nsdateformatter

我打算调用一个函数来检查从日期开始经过的时间。这将确定timeLable在我的消息视图控制器中的显示方式,类似于IMessage。

我在下面使用的代码只显示HH:MM

let date = dateFormatter().dateFromString((recent["date"] as? String)!)

timeLabel.text = NSDateFormatter.localizedStringFromDate(date!, dateStyle: NSDateFormatterStyle.NoStyle, timeStyle: NSDateFormatterStyle.NoStyle)

我希望将其更改为:

  • 如果日期是今天,日期=“HH:MM”
  • 如果日期是昨天,则日期=“昨天”
  • 如果日期是前天等,则日期=“星期一,星期二,星期三......”
  • 如果日期超过1周,则日期= MM / DD / YY

2 个答案:

答案 0 :(得分:1)

或试试这个。请注意,我们必须使用components:fromDate:,然后使用components:fromDateComponents:toDateComponents:options:,因为如果我们不在晚上23:59返回23:59而不是昨天。

extension NSDateFormatter {
    static func friendlyStringForDate(date:NSDate) -> String {
        // Fetch the default calendar
        let calendar = NSCalendar.currentCalendar()

        // Compute components from target date
        let from = calendar.components([.Day, .Month, .Year], fromDate: date)

        // Compute components from current date
        let to = calendar.components([.Day, .Month, .Year], fromDate: NSDate())

        // Compute days difference between the two
        let delta = calendar.components(.Day, fromDateComponents: from, toDateComponents: to, options: [])

        switch delta.day {
        case 0:
            let formatter = NSDateFormatter()
            formatter.timeZone = NSTimeZone.defaultTimeZone()
            formatter.dateFormat = "HH:mm"
            return formatter.stringFromDate(date)

        case 1:
            return "Yesterday"

        case 2..<7:
            let formatter = NSDateFormatter()
            formatter.timeStyle = .NoStyle
            formatter.dateFormat = "EEEE"
            return formatter.stringFromDate(date)

        default:
            let formatter = NSDateFormatter()
            formatter.timeStyle = .NoStyle
             formatter.dateFormat = "MM/dd/YY"
            return formatter.stringFromDate(date)
        }
    }
}

现在,只使用它:

timeLabel.text = NSDateFormatter.friendlyStringForDate(date!)

答案 1 :(得分:1)

SWIFT 3:

extension DateFormatter {

    static func friendlyStringForDate(date: Date) -> String {
        // Fetch the default calendar
        let calendar = Calendar.current
        let unitFlags: NSCalendar.Unit = [.day]

        // Compute days difference between the two
        let delta = (calendar as NSCalendar).components(unitFlags, from: date, to: Date(), options: [])

        if let day = delta.day {

            switch day {
            case 0:
                let formatter = DateFormatter()
                formatter.timeZone = NSTimeZone.default
                formatter.dateFormat = "hh:mm a"
                return formatter.string(from: date)

            case 1:
                return "Yesterday"

            case 2..<7:
                let formatter = DateFormatter()
                formatter.timeStyle = .none
                formatter.dateFormat = "EEEE"
                return formatter.string(from: date)

            default:
                let formatter = DateFormatter()
                formatter.timeStyle = .none
                formatter.dateFormat = "MM/dd/YY"
                return formatter.string(from: date)
            }
        }
        return ""
    }
}