本文介绍了Swift 如何转换 Parse createdAt 时不时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Parse 中检索了 createdAt 列,并将它写在 UITableview 上的每一行,但时间和日期在 tableview 上看起来像这样:

I've retrieved createdAt column from Parse and i wrote it on UITableview for each row, but the time and date looks like this on tableview :

23-12-2015 01:04:56.380

23-12-2015 01:04:56.380

28-08-2015 19:45:09.101

28-08-2015 19:45:09.101

例如,我想将其转换为之前的时间:

I want to convert that to time ago for example:

今天下午 1:04 -- 昨天上午 5:24 -- 3 天前 -- 1 周前 -- 1 个月前

Today 1:04 PM -- Yestarday 5:24 AM -- 3 days ago -- 1 week ago -- 1 month ago

这是我的代码:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss.SSS"
let date = dateFormatter.stringFromDate((msg.createdAt as NSDate?)!)
Cell.timelbl.text = date

我使用的是 xcode 7 测试版

I'm using xcode 7 beta

有什么帮助吗?

推荐答案

您可以使用 NSCalendar isDateInToday 来检查 createdAt 日期是否与今天在同一天,并使用 isDateInYesterday 来检查是否是昨天.只需添加一个条件并为这些条件返回一个自定义字符串,对于所有其他条件,只需让日期组件格式化程序为您处理即可.

You can use NSCalendar isDateInToday to check if createdAt date is in same day as today, and use isDateInYesterday to check if it was yesterday. Just add a conditional and return a custom string for those conditions, for all other conditions just let date components formatter take care of it for you.

extension Formatter {
        static let time: DateFormatter = {
            let formatter = DateFormatter()
            formatter.calendar = Calendar(identifier: .gregorian)
            formatter.dateFormat = "h:mm a"
            return formatter
        }()
    static let dateComponents: DateComponentsFormatter = {
        let formatter = DateComponentsFormatter()
        formatter.calendar = Calendar(identifier: .iso8601)
        formatter.unitsStyle = .full
        formatter.maximumUnitCount = 1
        formatter.zeroFormattingBehavior = .default
        formatter.allowsFractionalUnits = false
        formatter.allowedUnits = [.year, .month, .weekOfMonth, .day, .hour, .minute, .second]
        return formatter
    }()
}
extension Date {

    var time: String { return Formatter.time.string(from: self) }

    var year:    Int { return Calendar.autoupdatingCurrent.component(.year,   from: self) }
    var month:   Int { return Calendar.autoupdatingCurrent.component(.month,  from: self) }
    var day:     Int { return Calendar.autoupdatingCurrent.component(.day,    from: self) }

    var elapsedTime: String {
        if timeIntervalSinceNow > -60.0  { return "Just Now" }
        if isInToday                 { return "Today at (time)" }
        if isInYesterday             { return "Yesterday at (time)" }
        return (Formatter.dateComponents.string(from: Date().timeIntervalSince(self)) ?? "") + " ago"
    }
    var isInToday: Bool {
        return Calendar.autoupdatingCurrent.isDateInToday(self)
    }
    var isInYesterday: Bool {
        return Calendar.autoupdatingCurrent.isDateInYesterday(self)
    }
}

测试:

Calendar.autoupdatingCurrent.date(byAdding: .second, value: -59, to: Date())!
    .elapsedTime        // "Just Now"

Calendar.autoupdatingCurrent.date(byAdding: .minute, value: -1, to: Date())!
    .elapsedTime        // "Today at 5:03 PM"
Calendar.autoupdatingCurrent.date(byAdding: .hour, value: -1, to: Date())!
    .elapsedTime        // "Today at 4:04 PM"

Calendar.autoupdatingCurrent.date(byAdding: .day, value: -1, to: Date())!
    .elapsedTime        // "Yesterday at 5:02 PM"
Calendar.autoupdatingCurrent.date(byAdding: .weekOfYear, value: -1, to: Date())!
    .elapsedTime        // "1 week ago"

Calendar.autoupdatingCurrent.date(byAdding: .month, value: -2, to: Date())!
    .elapsedTime        // "2 months ago"
Calendar.autoupdatingCurrent.date(byAdding: .year, value: -1, to: Date())!
    .elapsedTime        // "1 year ago"

这篇关于Swift 如何转换 Parse createdAt 时不时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 23:22