【问题标题】:Swift How to convert Parse createdAt time to time ago?Swift 如何转换 Parse createdAt time before time?
【发布时间】:2015-12-23 16:06:02
【问题描述】:

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

23-12-2015 01:04:56.380

28-08-2015 19:45:09.101

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

今天下午 1:04 -- 昨天凌晨 5:24 -- 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 测试版

有什么帮助吗?

【问题讨论】:

标签: swift uitableview parse-platform nsdate


【解决方案1】:

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

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"

【讨论】:

  • 我真的很感激,但我仍然遇到一些代码问题,我刚收到新消息,它说“-14 小时前”,所以这意味着秒\分钟前没有工作它一直显示“ -14 小时前”你能帮我解决这个问题吗?非常感谢!
  • 您需要提供更多信息,返回 -14 小时的日期是什么时候?它必须在未来。
  • 当我在早上 5 点发送消息时,它可能显示“-14 小时前”,但是当我在早上 6 点发送时它显示“今天早上 6 点”,请看这里:i.gyazo.com/ff3770c53dbf67f1b34d902b5955fd31.png,你能帮帮我吗让它“几秒钟前 - 一小时前 - 3 小时前 - 然后今天 - 昨天 - 一周前 - 一个月前”?
  • @Salah Z 表示 UTC 时间而不是您的当地时间。距格林威治标准时间零秒
  • 您只需要添加一个 IF 条件检查时间间隔是否小于 4 小时就返回日期组件格式化字符串
【解决方案2】:

哇,上面的答案对我来说工作量太大了!这就是我所做的!我想你会发现这对你来说不那么头疼了!

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

如果您使用的是 tableView,它看起来像您。我就是这样做的。在“属性检查器”下的Interface Builder中为您的标签分配一个标签确保您选择了标签)。

在您的 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;

【讨论】:

  • 非常感谢,但是你能在 Swift 上做到这一点吗?因为我只使用 swift 而我不知道目标 C。
  • 天哪,对不起兄弟……在这种情况下,请查看这个 github 项目:github.com/Toldy/swift-time-ago
  • 我不擅长 swift...但我认为这行得通?您可能需要稍微弄乱它才能使其正常工作,因为我只是在其中潦草地写了:let dateLabel = cell.viewWithTag(103) as? UILabel let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss" let parseDate = dateFormatter .stringFromDate(object.createdAt) let timeAgoFormattedDate = NSDate dateTimeFormattedAsTimeAgo: parseDate dateLabel.text = @"%@", timeAgoFormattedDate
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 2022-12-02
  • 2023-03-17
  • 2021-12-03
  • 1970-01-01
相关资源
最近更新 更多