concept Timex in category elixir

appears as: Timex
Phoenix in Action

This is an excerpt from Manning's book Phoenix in Action.

  • Formatting a date and time with the Timex library
  • You’ll use Timex in your application. Add timex as a dependency for auction_web (if you’ve forgotten how, check out chapter 5; you can specify {:timex, "> 0.0.0"} to grab the most recent version). Then, run mix deps.get, and restart your application. It will now be available to use.

    I won’t go too much into using the module—you can read the documentation if you’d like deeper insight into the workings of Timex. You’ll use it in the AuctionWeb.GlobalHelpers module, and use Timex.format!/2 in a function there.

    Listing 11.28. Creating formatted_datetime as a global helper
    defmodule AuctionWeb.GlobalHelpers do
      use Timex
    
      def integer_to_currency(amount) do
        dollars_and_cents =
          amount
          |> Decimal.div(100)
          |> Decimal.round(2)
        "$#{dollars_and_cents}"
      end
    
      def formatted_datetime(datetime) do
        datetime
        |> Timex.format!("{YYYY}-{0M}-{0D} {h12}:{m}:{s}{am}")      #1
      end
    end
    sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage
    test yourself with a liveTest