Thank you! Been stuck on this all day!
Post
Replies
Boosts
Views
Activity
I posted my response in the answer below.
Sorry about that – I reposted.
Just to add to my last comment, let's look at the DateFormatter equivalent and see if this can be done with the new API: If we have a local user preference called isTwentyFourHourClock(a Bool) and I wanted to respect that setting I could use formatter.setLocalizedDateFormatFromTemplate(isTwentyFourHourClock ? "HHmm" : "hhmm"). Is there any equivalent to this with the date.formatted() method?
Thanks for getting back to me. So, let me try to explain what "it" is again. In a nutshell, I want to use the new date formatting API to display hours and minutes, but I want to control whether the hours are displayed in 12h format or 24h format. This can be done with DateFormatter but I'm currently trying to update some code: I want to replace all the DateFormatter usage with the new API.
As an example, I have code using the new API such as date.formatted(formatStyle.hour(.defaultDigits(amPM: .abbreviated)).minute()) where formatStyle is something like in my original question and date is just a date instance. However, I see no way to control whether the hour digits are provided as 12h or 24h format – they, understandably, come as what is relevant to the user locale by default. So, for example, a user in the US will see 12h digits, and a user in France will see 24h digits. But what I really want to do is respect a local user setting: I have a boolean toggle in my app that allows the user to select their preference (12h vs 24h clocks). So, if the user has selected that they prefer the 24h format, I want to force the new API to always give me 24h format, and similarly if the user selects the 12h format, I want the new API to always provide the 12h format, no matter what the device locale is. But I so far cannot find a way to do this.
To answer your other questions:
You’re displaying a time to the user? Yes.
You’re trying to display 24-hour time regardless of the user’s preferences? I'm trying to respect the user's local in-app preference for 12h vs 24h formats.
Do you absolutely require a two digit hour? Or do you want the width of any hours before 10 to vary by locale?
I actually have a lot of different widgets in my app that display the current time. In some cases I display hours that are zero padded and some that are not necessarily padded. With the new API I see this can be done using .hour(.twoDigits(amPM: .abbreviated)) vs .hour(.defaultDigits(amPM: .abbreviated)).
In the 12-hour case, do you want the am/pm markers?
Similarly for the am/pm markers, in some cases I show them, in others they are hidden. This is easily controlled with the Date.FormatStyle.Symbol.Hour.AMPMStyle options.
Hello, and thanks for your reply.
Actually, I do not want to use DateFormatter at all, and using the format "HH:mm" was just an example here.
What I want to do is replace the usage of DateFormatter in my code with the new iOS 15 API date.formatted() method.
The problem that I'm having is figuring out how to force the new API to display 12 vs 24h times. So, for example, if the user has opted for the 24-hour clock (via a boolean setting in the app), how can I ensure that date.formatted() will always display the times in 24h, no matter what the device locale is?
Perfect, thank you!