A simple way to generate date formatting templates and localized date strings

What's New

Release 0.1.0


Initial release 🎉


DateTemplates is a library that provides a simple way to generate date formatting templates that can be use 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 seamesly with DateFormatter. Automatic support for localization makes it easier than ever to work with custom date formats.

Declarative Syntax

DateTemplates uses a declarative syntax so you can simply state which elements should be included in the formatted date string. For example, you can write that you want a date consisting of full week day, and time, without having to worry about template symbols, localization discrepancies, or clock-format.

let template = DateTemplate().dayOfWeek(.full).time()
print(template.localizedString(from: Date()))

This template will render dates as follows:

Locale Formatted String
"en_US" "Thursday 12:00 AM"
"es_ES" "jueves, 0:00"
"ja_JP" "木曜日 0:00"
"ru_RU" "четверг 00:00"
"ar_EG" "الخميس ١٢:٠٠ ص"


The following examples assume "America/Los_Angeles" time-zone and "en_US" locale:

Template Formatted String
DateTemplate().time().timeZone() "12:00 AM PST"
DateTemplate().dayOfWeek().day().month(.abbreviated).year() "Thu, Jan 1, 1970"
DateTemplate().day().month(.abbreviated).year(length: 2).era() "Mar 15, 44 BC"

Under the Hood

A DateTemplate instance provides a declarative way to composing date formatting template strings. These are regular, standard templates that can be used with DateFormatter.

let dateTemplate = DateTemplate().year().month().day().hours().minutes()
print(dateTemplate.template) // "yMdjmm"

These template strings can be used to generate a localized date format:

let template = DateTemplate().year().month().day().hours().minutes().template
let format = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: nil) ?? template
print(format) // "M/d/y, h:mm a" (assuming en_US locale)

For convenience, DateTemplate provides a localizedFormat method:

let template = DateTemplate().year().month().day().hours().minutes()
let format = template.localizedFormat()
print(format) // "M/d/y, h:mm a" (assuming en_US locale)

Localized format strings can be used with DateFormatter to convert dates to strings (and viceversa):

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US")
formatter.dateFormat = format // "M/d/y, h:mm a" 
formatter.timeZone = TimeZone(secondsFromGMT: 0)
let string = formatter.string(from: Date(timeIntervalSince1970: 0))
print(string) // "1/1/1970, 12:00 AM"

For convenience, DateTemplate provides a localizedString method:

let template = DateTemplate().year().month().day().hours().minutes()
let string = template.localizedString(from: Date(timeIntervalSince1970: 0),
                                      locale: Locale("en_US"), 
                                      timeZone: TimeZone(secondsFromGMT: 0))
print(string) // "1/1/1970, 12:00 AM"

Benefits of Using DateTemplates

These are some benefits:


For Xcode projects

Add Swift Package to Xcode via File -> Swift Packages -> Add Package Dependency...

For Swift Packages

Add DateTemplates dependency to Packages.swift

dependencies: [
    // other dependencies
    .package(url: "", from: "0.1.0")
targets: [
    .target(name: "YourPackage", dependencies: [
        // other dependencies
    // other targets


MIT License

Copyright (c) 2020 Eneko Alonso


  • Swift Tools 5.0.0
View More Packages from this Author


  • None
Last updated: Wed Nov 09 2022 01:59:17 GMT-0500 (GMT-05:00)