Class
MessagesDateFormatter
public class MessagesDateFormatter
A helper class with a contained DateFormatter that formats dates for the MessagesViewController.
This object can be used outside of the MessagesViewController if you'd like to use the same formatting that it does.
Properties
Methods
string(from:)
public func string(from date: Date) -> String
Creates a formatted string from a date.
Examples
-
If date is today, this would render a string containing only the time; 3:30 PM
-
If the date was yesterday, this would render a string containing the date and time; 11/30/2020 3:30 PM
-
If the date is in the same week, this would render a string containing the weekday and time; Wednesday 3:30 PM
-
If the date is in the same year, this would render a string as such: Fri, 8 Jan, 3:30 PM
-
Otherwise, it would render Jan 8, 2020, 3:30 PM
Parameters
Name | Type | Description |
---|---|---|
date | Date |
The date to format. |
Returns
The formatted string.
relativeDayString(from:)
public func relativeDayString(from date: Date) -> String
Creates a formatted string from a date containing only the day.
This will perform relative date formatting for a date that is either today or yesterday.
Examples
-
If date is today or yesterday, this would render a string like,
Today
orYesterday
-
If the date is in the same week, this would render a string containing the weekday; Wednesday
-
If the date is in the same year, this would render a string as such: Fri, 8 Jan
-
Otherwise, it would render Jan 8, 2020
Parameters
Name | Type | Description |
---|---|---|
date | Date |
The date to format. |
Returns
The formatted string.
attributedString(from:with:)
public func attributedString(from date: Date, with attributes: [NSAttributedString.Key: Any]) -> NSAttributedString
Creates a formatted attributed string from a date.
This method produces the same strings as string(from:)
but as an NSAttributedString
Parameters
Name | Type | Description |
---|---|---|
date | Date |
The date to format. |
attributes | [NSAttributedString.Key: Any] |
The attributes to apply to the string. |
Returns
An NSAttributedString of the formatted date.