2011-06-13 10 views
12

我想以「人性化」的方式顯示NSDates,例如「上週」或「幾天前」。類似於Pretty Time for JavaiOS上使用NSDate的人性化日期說明

什麼是最好的方式來做到這一點,子類化NSDateFormatter?在我開始重新發明輪子之前,是否已經有這樣的圖書館?

+0

檢查這些類似問題的答案:[是否有可可以用自然語言顯示時間間隔的某些功能?](http://stackoverflow.com/questions/2181034/is-there-some-functionality-in -cocoa-to-display-time-intervals-in-natural-languag) - [有沒有方法將自然語言日期NSString轉換爲NSDate](http://stackoverflow.com/questions/5878528/is-there -a-way-to-convert-a-natural-language-date-nsstring-to-an-nsdate) – djromero 2011-06-13 15:31:00

回答

3

這是夫特2溶液:

func formattedHumanReadable(date: NSDate) -> String { 
    let formatter = NSDateFormatter() 
    formatter.timeStyle = .NoStyle 
    formatter.dateStyle = .ShortStyle 
    formatter.doesRelativeDateFormatting = true 

    let locale = NSLocale.currentLocale() 
    formatter.locale = locale 

    return formatter.stringFromDate(date) 
    } 
37

在iOS 4及更高版本,使用doesRelativeDateFormatting屬性:

NSDateFormatter *dateFormatter = ...; 
dateFormatter.doesRelativeDateFormatting = YES; 
+4

這是完美的。以下是文檔:http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/setDoesRelativeDateFormatting: – Joony 2012-07-03 06:37:34

+0

輝煌!謝謝。 – anticyclope 2012-10-25 10:31:46

+0

如果你想要一個時間間隔的自然語言表示,這是完美的。例如。 iOS上的iTunes wifi同步設置視圖顯示「上次同步:昨天下午1:06」 – AFraser 2013-01-15 23:50:16

7

Three20的NSDateAdditions:

https://github.com/pbo/three20/blob/master/src/Three20Core/Sources/NSDateAdditions.m

..讓你做這一點。

編輯:在2013年,你真的不想再使用Three20了。使用Regexident的解決方案。在Xcode

+1

Three20是**沒有本地化**雖然。鑑於蘋果公司的'NSDateFormatter'尊重當地的差異(例如法國「後天的後一天」,請參見[這裏](http://developer.apple。COM /庫/ IOS /文檔/可可/參考/基礎/班/ NSDateFormatter_Class /參考/#的reference.html // apple_ref/OCC/instm/NSDateFormatter/setDoesRelativeDateFormatting :))。從長遠來看,只用英語通常是一個壞主意。 – Regexident 2011-06-13 15:20:44

+0

@regexident你是對的。當然,由於NSDateAdditions是開源的,你可以對它進行本地化,並且你可以更好地控制它的行爲方式,而NSDateFormatter可以爲你做,並且解決你的問題。不過,不僅如此,我認爲他們的表現完全不同,他們的產出也有所不同。 Three20是 - 當然 - 更多àla-facebook。好的觀察,但。 – magma 2011-06-13 15:28:35

+0

Three20開源是一個很好的觀點(和優勢,毫無疑問,但也是一個責任)。然而,NSDataFormatter具有始終符合(並且符合)(可能是移位)操作系統標準的優勢(imho),而不需要將來有一天會進行全面重寫。這一點,加上自定義格式化程序造成的混亂可能性。對於諸如「今天/明天/昨天」之類的微不足道的事情,這不應該是一個大問題,但仍然值得我們考慮,imho。畢竟你想要的最後一件事是讓你的客戶困惑你的應用程序。 – Regexident 2011-06-13 15:35:06

5

完整的代碼片段爲人類可讀日期:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; 
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 

    NSLocale *locale = [NSLocale currentLocale]; 
    [dateFormatter setLocale:locale]; 

    [dateFormatter setDoesRelativeDateFormatting:YES]; 
+0

只是爲了澄清,因爲這是最好的答案。之後,我們需要做一些像[dateFormatter stringFromDate:mydate]; – 2017-11-22 14:40:06

1

一個很好的日期格式是YLMoment,這是基於流行的moment.js

它的格式不錯,相對時間。

0

使用DateTools(GitHub的/的CocoaPods)timeAgoSinceNow功能。下面是一些示例輸出...

NSDate.init(timeIntervalSinceNow:-3600).timeAgoSinceNow() "An hour ago" NSDate.init(timeIntervalSinceNow:-3600*24).timeAgoSinceNow() "Yesterday" NSDate.init(timeIntervalSinceNow:-3600*24*6).timeAgoSinceNow() "6 days ago" NSDate.init(timeIntervalSinceNow:-3600*24*7*3).timeAgoSinceNow() "3 weeks ago" NSDate.init(timeIntervalSinceNow:-3600*24*31*3).timeAgoSinceNow() "3 months ago"

timeAgoSinceDate功能也很方便。

DateTools支持許多(人類)語言,並提供比NSDateFormatter的有限doesRelativeDateFormatting更好的相關描述。