Swift如何转换Parse createdAt time to time?

时间:2015-12-23 16:06:03

标签: swift uitableview parse-platform nsdate

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

23-12-2015 01:04:56.380

28-08-2015 19:45:09.101

我想将其转换为时间段,例如:

今天下午1:04 - Yestarday 5:24 AM - 3天前 - 1周前 - 1个月之前

这是我的代码:

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 beta

请帮忙吗?

2 个答案:

答案 0 :(得分:0)

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

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"

答案 1 :(得分:0)

哇,上面的答案对我来说太过分了!这就是我做的!我想你会发现这不是你头疼的问题!

首先从Github上的Kevin Lawler下载并导入NSDate-TimeAgo到您的项目中。

如果您使用的是tableView,它看起来就像您一样。这就是我做到的。在 Inspector 下的 Interface Builder 中为标签分配标记确保选择了标签)。

Attributes Inspector

tableViewController.m 文件中,您需要找到 cellForRowAtIndexPath ,因为您正在使用Parse,我假设您正在使用 PFQueryTableViewController ,所以你将把你的代码放在里面:

- (PFTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object: (PFObject *)object
{
}

NSDate + TimeAgo.h 导入 tableView.h 文件,然后将代码放在大括号内,确保返回你的单元格注意:确保你有对PFObject的引用,如上面的代码

///////////////DATE LABEL (WHEN CREATED)//////////////
//Reference to label in interface builder//
UILabel *myDateLabel = (UILabel*) [cell viewWithTag:103];
//reference to PFObject date of creation from Parse//
NSDate *createdAt = object.createdAt;
//create a string from timeAgo and Parse date//
NSString *timeAgo = [createdAt timeAgo];
//assign string to your label//
myDateLabel.text = timeAgo;