迅捷 - 比较今天和过去的日期

时间:2017-04-28 03:48:21

标签: swift date

我是Swift的新手。我对约会有疑问。

如果我得到的日期字符串类似于2016-01-25T10:04:53.498Z'但我想比较现在和日期字符串的日期。我该怎么办?

我以TSWeChat为参考。

let currentDate = NSDate() 

func week() -> String {
    let myWeekday: Int = (Calendar.current as NSCalendar).components([NSCalendar.Unit.weekday], from: self).weekday!
    switch myWeekday {
    case 0:
        return "Sun"
    case 1:
        return "Mon"
    case 2:
        return "Tue"
    case 3:
        return "Wed"
    case 4:
        return "Thu"
    case 5:
        return "Fri"
    case 6:
        return "Sat"
    default:
        break
    }
    return "none"
}

var chatTimeString: String {
    get {
        let calendar = Calendar.current
        let now = Date()
        let unit: NSCalendar.Unit = [
            NSCalendar.Unit.minute,
            NSCalendar.Unit.hour,
            NSCalendar.Unit.day,
            NSCalendar.Unit.month,
            NSCalendar.Unit.year,
        ]
        let nowComponents:DateComponents = (calendar as NSCalendar).components(unit, from: now)
        let targetComponents:DateComponents = (calendar as NSCalendar).components(unit, from: self)

        let year = nowComponents.year! - targetComponents.year!
        let month = nowComponents.month! - targetComponents.month!
        let day = nowComponents.day! - targetComponents.day!

        if year != 0 {
            return String(format: "%zdY%zdM%zdD %02d:%02d", targetComponents.year!, targetComponents.month!, targetComponents.day!, targetComponents.hour!, targetComponents.minute!)
        } else {
            if (month > 0 || day > 7) {
                return String(format: "%zdM%zdD %02d:%02d", targetComponents.month!, targetComponents.day!, targetComponents.hour!, targetComponents.minute!)
            } else if (day > 2) {
                return String(format: "%@ %02d:%02d",self.week(), targetComponents.hour!, targetComponents.minute!)
            } else if (day == 2) {
                if targetComponents.hour! < 12 {
                    return String(format: "The day before yesterday A.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                } else if targetComponents.hour == 12 {
                    return String(format: "The day before yesterday P.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                } else {
                    return String(format: "The day before yesterday P.M %02d:%02d",targetComponents.hour! - 12, targetComponents.minute!)
                }
            } else if (day == 1) {
                if targetComponents.hour! < 12 {
                    return String(format: "Yesterday A.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                } else if targetComponents.hour == 12 {
                    return String(format: "Yesterday P.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                } else {
                    return String(format: "Yesterday P.M %02d:%02d",targetComponents.hour! - 12, targetComponents.minute!)
                }
            } else if (day == 0){
                if targetComponents.hour! < 12 {
                    return String(format: "A.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                } else if targetComponents.hour == 12 {
                    return String(format: "P.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                } else {
                    return String(format: "P.M %02d:%02d",targetComponents.hour! - 12, targetComponents.minute!)
                }
            } else {
                return ""
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

[基于此问题的标题。]不要比较日期组件。直接比较日期。如果您有日期字符串,请使用DateFormatter将其转换回日期。

  2> var past = Date ()
past: Date = 2017-04-28 03:59:08 UTC
  3> var now = Date()
now: Date = 2017-04-28 03:59:15 UTC
  4> now > past
$R0: Bool = true

答案 1 :(得分:1)

为了比较日期字符串到现在 - 将字符串转换为Date对象,然后将其与现在进行比较;由Date()初始化。

let dateString = "2016-01-25T10:04:53.498Z"

// Setup a date formatter to match the format of your string
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

// Create a date object from the string
if let date = dateFormatter.date(from: dateString) {

    if date < Date() {
        print("Before now")
    } else {
        print("After now")
    }
}