Format Time.utc_now() as {HH}:{mm} {a} (e.g. "9:45 am")

Between Elixir Time and Timex I can’t seem to figure out how to get a simple time and format it conventionally. Does anyone know how to do this? I’ve read a bunch of posts that suggest converting Time to erlang time to Timex.DateTime then formatting, but I just cant get it to work

Have you tried Timex.format(yourtime, "{h12}:{mm} {am}")?

1 Like

The example (9:45 am) doesn’t match your format spec. For double HH, I’d expect to see a leading zero for single digit hours.

But under the assumption that you use the default formatter, @Nicd’s looks right on a first glance, but it doesn’t understand {mm}, so given your example, the specifier "{h12}:{0m} {am}" should produce it, if though you want a zeropadded hour as I’d expect from HH in other libraries, you need to use {0h12} instead.

2 Likes

yup, @Nicd solved it

Time.utc_now()
|> Timex.format("{h12}:{0m} {am}")
1 Like