CreationKit Documentation

Class Messages​Date​Formatter

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

shared

public static let shared  

The shared MessagesDataFormatter object

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

date Date

The date to format.

Returns

The formatted string.

relative​Day​String(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 or Yesterday

  • 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

date Date

The date to format.

Returns

The formatted string.

attributed​String(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

date Date

The date to format.

attributes [NSAttributed​String.​Key:​ Any]

The attributes to apply to the string.

Returns

An NSAttributedString of the formatted date.