Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Talk about the “j” format template #35

Open
Frizlab opened this issue Dec 27, 2021 · 0 comments
Open

Talk about the “j” format template #35

Frizlab opened this issue Dec 27, 2021 · 0 comments

Comments

@Frizlab
Copy link

Frizlab commented Dec 27, 2021

This one is a weirdo, but very cool.
It is not available on a formatter’s dateFormat directly, instead it exists on the class method dateFormat(fromTemplate:, options:, locale:), and thus on the instance method setLocalizedDateFormatFromTemplate(_:).

Setting the template to j on a formatter will return the hour either in 24-hour time or am/pm depending on the locale.
An example

import Foundation
let formatter = DateFormatter()

formatter.locale = Locale(identifier: "fr")
formatter.setLocalizedDateFormatFromTemplate("j")
print(formatter.string(from: Date())) /* -> "19 h" */

/* Same example with a different locale */
formatter.locale = Locale(identifier: "en_US")
formatter.setLocalizedDateFormatFromTemplate("j") /* Must be set again after changing the locale. */
print(formatter.string(from: Date())) /* -> "7 PM" */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant