Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dilipiOSDeveloper/9ddd041b027fe3ec97243a20f89536c0 to your computer and use it in GitHub Desktop.
Save dilipiOSDeveloper/9ddd041b027fe3ec97243a20f89536c0 to your computer and use it in GitHub Desktop.
write this code in method.
let message = messages[indexPath.row]
if indexPath.row == 0 {
return NSAttributedString(string: message.date.dateString(), attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 13.0, weight: .bold)])
}else {
let previousMessage = messages[indexPath.row - 1]
if Calendar.current.isDate(previousMessage.date, inSameDayAs: message.date) {
return nil
}else {
return NSAttributedString(string: message.date.dateString(), attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 13.0, weight: .bold)])
}
}
And copy this outside of your class
public extension Date {
public func dateString (format: String = "dd MMM, yyyy") -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment