Swift Firebase格式化日期无法正常工作

时间:2019-01-02 07:08:22

标签: ios swift firebase

我正在格式化Firebase的时间戳,将时间分解为年,月,日,小时和分钟。我正在使用另一种日期格式化程序,它可以将时间分解为仅几小时和几分钟的确切时间。只是试图寻求一些帮助,以了解为什么它不是更详细的日期格式化程序是行不通的。

详细格式化程序

extension Date {

func getElapsedInterval() -> String {

    let interval = Calendar.current.dateComponents([.year, .month, .day], from: self, to: Date())

    if let year = interval.year, year > 0 {
        return year == 1 ? "\(year)" + " " + "year" :
            "\(year)" + " " + "years"
    } else if let month = interval.month, month > 0 {
        return month == 1 ? "\(month)" + " " + "month" :
            "\(month)" + " " + "months"
    } else if let day =  interval.day, day > 0 {
        return day == 1 ? "\(day)" + " " + "day" :
            "\(day)" + " " + "days"
    } else if let hour = interval.hour, hour > 0 {h
        return hour == 1 ? "\(hour)" + " " + "hour" :
            "\(hour)" + " " + "hours"
    } else if let minute =  interval.minute, minute > 0 {
        return minute == 1 ? "\(minute)" + " " + "minute" :
            "\(minute)" + " " + "minutes"
    }

    return "a moment ago"
  }
}

工作日期格式化程序

if let seconds = group?.timestamp {

        let timestampDate = NSDate(timeIntervalSince1970: TimeInterval(seconds))
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "h:mm a"
        self.messageTime.text = dateFormatter.string(from: timestampDate as Date)
    }

日期格式化程序不正确

if let seconds = group?.timestamp?.doubleValue {
        let timestampDate = Date(timeIntervalSince1970: seconds)
        self.messageTime.text = timestampDate.getElapsedInterval()
    }

即使我正在测试相同的数据,使用详细日期格式化程序的无法使用的日期格式化程序仍会继续在标签上打印“片刻之前”。

0 个答案:

没有答案