Help us understand the problem. What is going on with this article?

[Swift]TwitterやFacebookの様に、日時を「n秒前」や「n日前」で表示するextension

More than 3 years have passed since last update.

SNSなどの日時表示で使われているアレです。簡易的ではありますが、お役に立てられれば嬉しいです。

※以下の関数をswiftで書き直させていただいたものになります :bow:
fuzzy_time.php (Gist)

実行例

現在日時を2015/5/3 18:00:00とした場合、

test.swift
println(NSDate.parse("2015-05-03T18:00:00+09:00").toFuzzy()) // 0秒前
println(NSDate.parse("2015-05-03T17:59:59+09:00").toFuzzy()) // 1秒前
println(NSDate.parse("2015-05-03T17:30:00+09:00").toFuzzy()) // 30分前
println(NSDate.parse("2015-05-01T18:000:00+09:00").toFuzzy()) // 2日前
println(NSDate.parse("2015-01-01T18:000:00+09:00").toFuzzy()) // 1月1日
println(NSDate.parse("2014-05-03T18:000:00+09:00").toFuzzy()) // 2014年5月3日

仕様

  • 1分未満は n秒前
  • 1時間未満は n分前
  • 1日未満は n時間前
  • 31日以内は n日前
  • 32日以上前の場合は M月d日
  • 32日以上前で、現在と年が違う場合は yyyy年M月d日

コード

extension.swift
extension NSDate {
    func toFuzzy() -> String {
        let now = NSDate()

        let cal = NSCalendar.currentCalendar()
        let calUnit: NSCalendarUnit = .CalendarUnitSecond | .CalendarUnitMinute | .CalendarUnitHour | .CalendarUnitDay | .CalendarUnitYear
        let components = cal.components(calUnit, fromDate: self, toDate: now, options: nil)

        let diffSec = components.second + components.minute*60 + components.hour*3600 + components.day*86400 + components.year*31536000
        var result = String()

        if diffSec < 60 {
            result = "\(diffSec)秒前"
        } else if diffSec < 3600 {
            result = "\(diffSec/60)分前"
        } else if diffSec < 86400 {
            result = "\(diffSec/3600)時間前"
        } else if diffSec < 2764800 {
            result = "\(diffSec/86400)日前"
        } else {
            let dateFormatter = NSDateFormatter()

            if components.year > 0 {
                dateFormatter.dateFormat = "yyyy年M月d日"
                result = dateFormatter.stringFromDate(self)
            } else {
                dateFormatter.dateFormat = "M月d日"
                result = dateFormatter.stringFromDate(self)
            }
        }

        return result
    }

    class func parse(dateString:String) -> NSDate {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZZ"
        let d = formatter.dateFromString(dateString)
        return NSDate(timeInterval: 0, sinceDate: d!)
    }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした