用户多久前创建了帖子NSDate(),以操纵“发布时间"和“发布时间"的格式.被创造 [英] How long ago did the User create a post, NSDate(), Manipulating the format of the "time of post" created

查看:32
本文介绍了用户多久前创建了帖子NSDate(),以操纵“发布时间"和“发布时间"的格式.被创造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个社交应用程序,将其帖子保存在用户特定的节点中,与此同时,我还以这种格式保存了发布时间:-

I am making a social app that saves its posts in user specific nodes , with that i am also saving the time of post in this format :-

2016年7月20日,星期三,00:14

与我的帖子一起显示在用户朋友的全球供稿中.

which i display with the post in the global feed of friends of the user.

  • 在发布该帖子的24小时之前,我要在Feed上显示发布时间,如下所示:-"5小时前"

发布后24小时后,发布时间变成这样:-昨天" ...

After 24 hours of that post time of post becomes something like this :- "Yesterday"...

发布后48小时后,发布时间变成了这样的样子:-"8月5日" ...

After 48 hours of that post time of post becomes something like this :- "On 5 Aug"...

到目前为止,我已经提出了这两种选择:-

So far i have come up with these two options:-

1.)更改Feed在数据库中的时间,我认为这是更好的选择.

1.) Change the time of the feed in the database, which i think would be much better option.

2.)检索发布时间,通过MULTIPLE if 条件进行迭代,并相应地设置发布时间.

2.) Retrieve the time of post , iterate through MULTIPLE if conditions and set the time of post accordingly.

我可以实现第二个选项,但是我不知道如何继续选择一个选项

I would be able to implement the second option but i have no clue to how to go forward with option one

鉴于我的JSON树是这样的

Given that my JSON tree is something like this

appname:{

  users : {....
           .....

    user1 : {....
             .....

             postsCreated : {
                          post1 : {
                                     ..
                                     timeofPost : ""Wednesday, Aug 5, 2016, 00:14""      

                                  }
                     }
           }
     }
}

我确实偶然发现了 http://momentjs.com/,但是对于Java脚本而言

I did stumble upon http://momentjs.com/ but thats for Javascript

在我的JSON树上还有任何建议吗?还是这样?

Also any suggestion on my JSON tree or is it fine the way it is?

推荐答案

您建议:

  1. 更改数据库中feed的时间,我认为这是更好的选择.

否,数据库中的日期以及与Web服务通信的日期不应为带格式的字符串.数据库和Web服务应捕获原始日期(或更准确地说,是RFC3339/ISO8601格式或从某个参考日期开始的秒数).UI字符串中经过时间的格式是应用程序的责任.

No, the date in the database, as well as that which is communicated with web service, should not be a formatted string. The database and the web service should be capturing the raw dates (or, more accurately, RFC3339/ISO8601 format or seconds from some reference date). The formatting of the elapsed time in a string for the UI is the responsibility of the app.

  1. 获取发布时间,如果条件允许,请遍历MULTIPLE并相应地设置发布时间.

是的,这就是您应该做的.

Yes, that's what you should do.

顺便说一句,如果您要省略年份,那么您可能会有第四个排列,如果日期过去一年以上,则包括年份,例如:

By the way, if you're going to omit the year, you probably have a fourth permutation which includes year if the date is more than one year in the past, e.g.:

func formattedPostDateString(date: NSDate) -> String {
    let now = NSDate()
    let elapsed = NSCalendar.currentCalendar().components([.Day, .Year], fromDate: date, toDate: now, options: [])

    switch (elapsed.year, elapsed.day) {
    case (0, 0):
        return "\(elapsedFormatter.stringFromDate(date, toDate: now)!) \(agoDateString)"
    case (0, 1):
        return yesterdayString
    case (0, _):
        return "\(onDateString) \(lessThanOneYearFormatter.stringFromDate(date))"
    default:
        return "\(onDateString) \(moreThanOneYearFormatter.stringFromDate(date))"
    }
}

哪里

let onDateString = NSLocalizedString("On", comment: "prefix used in 'On 5 Aug'")
let agoDateString = NSLocalizedString("ago", comment: "suffix use in '4 hours ago'")
let yesterdayString = NSLocalizedString("Yesterday", comment: "showing 'date' where it's between 24 and 48 hours ago")

let elapsedFormatter: NSDateComponentsFormatter = {
    let formatter = NSDateComponentsFormatter()
    formatter.allowedUnits = [.Year, .Month, .Day, .Hour, .Minute, .Second]
    formatter.unitsStyle = .Full
    formatter.maximumUnitCount = 1
    return formatter
}()

let lessThanOneYearFormatter: NSDateFormatter = {
    let formatter = NSDateFormatter()
    formatter.dateFormat = NSDateFormatter.dateFormatFromTemplate("MMM d", options: 0, locale: nil)
    return formatter
}()

let moreThanOneYearFormatter: NSDateFormatter = {
    let formatter = NSDateFormatter()
    formatter.dateStyle = .MediumStyle
    return formatter
}()

您唯一需要做的就是将Web服务返回的字符串转换为 NSDate 对象.为此,Web服务可能应该以ISO 8601/RFC 3339格式(例如 2016-08-26T15:01:23Z 格式)返回发布日期.

The only thing you need to do is to convert the string returned by the web service into NSDate object. To that end, the web service should probably return the post date in ISO 8601/RFC 3339 format (e.g. 2016-08-26T15:01:23Z format).

要在Swift 2中创建ISO8601/RFC3339日期,

To create ISO8601/RFC3339 dates in Swift 2:

let isoDateFormatter: NSDateFormatter = {
    let formatter = NSDateFormatter()
    formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
    formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
    return formatter
}()

然后:

let string = isoDateFormatter.stringFromDate(date)

let date = isoDateFormatter.dateFromString(string)

或者在使用Swift 3的iOS 10+中,您可以使用新的 ISO8601DateFormatter :

Or in iOS 10+ using Swift 3, you can use the new ISO8601DateFormatter:

let isoDateFormatter = ISO8601DateFormatter()

这篇关于用户多久前创建了帖子NSDate(),以操纵“发布时间"和“发布时间"的格式.被创造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆