Mam datę "2014-07-02 20:57:38 +0000" i chcę sformatować ją jako "Dzisiaj o 20:57". Chcę tego, jeśli ciąg jest wczoraj, a następnie wyświetlić go jako "Wczoraj o 9:00 rano". Jeśli nie jest to dziś ani wczoraj, po prostu pokaż aktualną datę, np. "27/6 o 19:53".Jak wyświetlić datę z ludzkim językiem, np. "Dzisiaj o xx: xx pm", "Wczoraj o xx: xx am"?
Udało mi się uzyskać czas w formacie "8:57" z kodem poniżej.
var formatter : NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "h:mm a"
// message.createdAt is the date
let dateString = formatter.stringFromDate(message.createdAt)
println(dateString)
//output = 8:57 AM
Jednak, gdy użyję następującego kodu, zwraca pusty ciąg.
var formatter : NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "h:mm a"
formatter.doesRelativeDateFormatting = true //<-- This doesn't work
let dateString = formatter.stringFromDate(message.createdAt)
println(dateString)
//output = (nothing, its a blank string)
Jak wykonać tę pracę i wyświetlić "Dzisiaj" lub "Wczoraj" w Swift?
** Swift 3 Odpowiedź: ** https://stackoverflow.com/a/46027060/3400991 –