NimbleStrftime v0.1.1 NimbleStrftime View Source
Simple datetime formatting based on the strftime format found on UNIX-like systems.
Formatting syntax
The formatting syntax for strftime is a sequence of characters in the following format:
%<padding><width><format>
where:
%
: indicates the start of a formatted section<padding>
: set the padding (see below)<width>
: a number indicating the minimum size of the formatted section<format>
: the format iself (see below)
Accepted padding options
-
: no padding, removes all padding from the format_
: pad with spaces0
: pad with zeroes
Accepted formats
The accepted formats are:
Format | Description | Examples (in ISO) |
---|---|---|
a | Abbreviated name of day | Mon |
A | Full name of day | Monday |
b | Abbreviated month name | Jan |
B | Full month name | January |
c | Preferred date+time representation | 2018-10-17 12:34:56 |
d | Day of the month | 01, 12 |
f | Microseconds (does not support width and padding modifiers) | 000000, 999999, 0123 |
H | Hour using a 24-hour clock | 00, 23 |
I | Hour using a 12-hour clock | 01, 12 |
j | Day of the year | 001, 366 |
m | Month | 01, 12 |
M | Minute | 00, 59 |
p | "AM" or "PM" (noon is "PM", midnight as "AM") | AM, PM |
P | "am" or "pm" (noon is "pm", midnight as "am") | am, pm |
q | Quarter | 1, 2, 3, 4 |
S | Second | 00, 59, 60 |
u | Day of the week | 1 (Monday), 7 (Sunday) |
x | Preferred date (without time) representation | 2018-10-17 |
X | Preferred time (without date) representation | 12:34:56 |
y | Year as 2-digits | 01, 01, 86, 18 |
Y | Year | -0001, 0001, 1986 |
z | +hhmm/-hhmm time zone offset from UTC (empty string if naive) | +0300, -0530 |
Z | Time zone abbreviation (empty string if naive) | CET, BRST |
% | Literal "%" character | % |
Any other character will be interpreted as an invalid format and raise an error
Link to this section Summary
Functions
Formats received datetime into a string.
Link to this section Functions
format(date_or_time_or_datetime, string_format, user_options \\ [])
View SourceFormats received datetime into a string.
The datetime can be any of the Calendar types (Time
, Date
,
NaiveDateTime
, and DateTime
) or any map, as long as they
contain all of the relevant fields necessary for formatting.
For example, if you use %Y
to format the year, the datatime
must have the :year
field. Therefore, if you pass a Time
,
or a map without the :year
field to a format that expects %Y
,
an error will be raised.
Options
:preferred_datetime
- a string for the preferred format to show datetimes, it can't contain the%c
format and defaults to"%Y-%m-%d %H:%M:%S"
if the option is not received:preferred_date
- a string for the preferred format to show dates, it can't contain the%x
format and defaults to"%Y-%m-%d"
if the option is not received:preferred_time
- a string for the preferred format to show times, it can't contain the%X
format and defaults to"%H:%M:%S"
if the option is not received:am_pm_names
- a function that receives either:am
or:pm
and returns the name of the period of the day, if the option is not received it defaults to a function that returns"am"
and"pm"
, respectively:month_names
- a function that receives a number and returns the name of the corresponding month, if the option is not received it defaults to a function thet returns the month names in english:abbreviated_month_names
- a function that receives a number and returns the abbreviated name of the corresponding month, if the option is not received it defaults to a function thet returns the abbreviated month names in english:day_of_week_names
- a function that receives a number and returns the name of the corresponding day of week, if the option is not received it defaults to a function that returns the day of week names in english:abbreviated_day_of_week_names
- a function that receives a number and returns the abbreviated name of the corresponding day of week, if the option is not received it defaults to a function that returns the abbreviated day of week names in english
Examples
Without options:
iex> NimbleStrftime.format(~U[2019-08-26 13:52:06.0Z], "%y-%m-%d %I:%M:%S %p")
"19-08-26 01:52:06 PM"
iex> NimbleStrftime.format(~U[2019-08-26 13:52:06.0Z], "%a, %B %d %Y")
"Mon, August 26 2019"
iex> NimbleStrftime.format(~U[2019-08-26 13:52:06.0Z], "%c")
"2019-08-26 13:52:06"
With options:
iex> NimbleStrftime.format(~U[2019-08-26 13:52:06.0Z], "%c", preferred_datetime: "%H:%M:%S %d-%m-%y")
"13:52:06 26-08-19"
iex> NimbleStrftime.format(
...> ~U[2019-08-26 13:52:06.0Z],
...> "%A",
...> day_of_week_names: fn day_of_week ->
...> {"segunda-feira", "terça-feira", "quarta-feira", "quinta-feira",
...> "sexta-feira", "sábado", "domingo"}
...> |> elem(day_of_week - 1)
...> end
...>)
"segunda-feira"
iex> NimbleStrftime.format(
...> ~U[2019-08-26 13:52:06.0Z],
...> "%B",
...> month_names: fn month ->
...> {"январь", "февраль", "март", "апрель", "май", "июнь",
...> "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"}
...> |> elem(month - 1)
...> end
...>)
"август"