-1

I am trying to create a Widget with SwiftUI, and I want to use the ".timer" style from Text to show a countdown, but the result of the Text(entry.date, style: .timer) is 1.34.23.

I want to use : instead of . as separator.

And is it possible to add a leading zero so instead of this 1.34.23, I want this 01:34:23

timbre timbre
  • 12,648
  • 10
  • 46
  • 77
Kaiison
  • 41
  • 4
  • 1
    I use a timer style in a widget and could not find a way to add a leading zero. I do see colons as a separator. I think the separator depends on your locale and what is the standard format to use in that country. – Geoff Hackworth Apr 02 '23 at 10:55

1 Answers1

1

You can override the automatic environment locale as follows e.g.

Text(Date().addingTimeInterval(60), style: .timer)
    .environment(\.locale, Locale(identifier: "en_GB"))

60:00
Text(Date().addingTimeInterval(60), style: .timer)
    .environment(\.locale, Locale(identifier: "fi_FI"))

60.00

See here for how countries format times:

https://en.wikipedia.org/wiki/Date_and_time_representation_by_country

See here for iOS locale identifiers:

https://gist.github.com/jacobbubu/1836273

malhal
  • 26,330
  • 7
  • 115
  • 133