Last active
April 18, 2022 02:40
-
-
Save danhawkins/3e2bf2392cb01ebdbbed4fd5ff78e7c4 to your computer and use it in GitHub Desktop.
Live Helpers for DateTime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule QuiqupFleetWeb.LiveHelpers do | |
@moduledoc """ | |
Helpers for Web components used in live view | |
""" | |
import Phoenix.LiveView | |
import Phoenix.LiveView.Helpers | |
@doc """ | |
Renders a datetime using the browser based toLocaleString methods. | |
You must include the datetime you want to use as value can optionally provide format | |
- datetime (the full datetime) | |
- date (just the date) | |
- timeshort (just the time without seconds) | |
- time (just the time with seconds) | |
## Examples | |
<.datetime value="2022-04-14T22:00:00Z" format="datetime" /> | |
14/4/2022 22:00 | |
<.datetime value="2022-04-14T22:00:00Z" format="timeshort" /> | |
22:00 | |
""" | |
def datetime(%{value: datetime} = assigns) do | |
format = Map.get(assigns, :format, "datetime") | |
locale_fn = | |
case format do | |
"datetime" -> "toLocaleString([],{dateStyle: 'short', timeStyle: 'short'})" | |
"date" -> "toLocaleDateString()" | |
"timeshort" -> "toLocaleTimeString([],{timeStyle: 'short'})" | |
"time" -> "toLocaleTimeString()" | |
_ -> "toLocaleString()" | |
end | |
~H""" | |
<span class="datetime" id={assigns.id} phx-update="ignore" x-data={"{date: new Date('#{datetime}')}"} x-text={"date.#{locale_fn}"}> | |
</span> | |
""" | |
end | |
def datetime(assigns) do | |
~H""" | |
""" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment