DEV Community

Cover image for Using DateTemplates in SwiftUI for custom date formatting
Eneko Alonso
Eneko Alonso

Posted on

Using DateTemplates in SwiftUI for custom date formatting

DateTemplates is a library that provides a simple way to generate date formatting templates that can be used to format dates in all Apple platforms and Linux. With a declarative Swift syntax that's easy to read and natural to write, DateTemplate works seamlessly with DateFormatter.

In SwiftUI, we can use it to easily format dates inline with out view code:

Text(
    DateTemplate()
        .dayOfWeek(.full)
        .month(.abbreviated)
        .day()
        .time()
        .localizedString(from: entry.timestamp)
)

Using DateTemplate in SwiftUI

Visit https://github.com/eneko/DateTemplates for more examples and documentation.

Top comments (0)