kip
kday is a simple, no-dependencies library to calculate the date of the first, last or nth day of the week on, nearest, before or after a given date. The code is extracted from ex_cldr_calendars so has been in production for several years and therefore considered stable.
Functions
-
first_kday(date, k) - Return the date of the first
day_of_weekon or after the specifieddate. -
kday_after(date, k) - Return the date of the
day_of_weekafter the specifieddate. -
kday_before(date, k) - Return the date of the
day_of_weekbefore the specifieddate. -
kday_nearest(date, k) - Return the date of the
day_of_weeknearest the specifieddate. -
kday_on_or_after(date, k) - Return the date of the
day_of_weekon or after the specifieddate. -
kday_on_or_before(date, k) - Return the date of the
day_of_weekon or before the specifieddate. -
last_kday(date, k) - Return the date of the last
day_of_weekon or before the specifieddate. -
nth_kday(date, n, k) - Return the date of the
nthday_of_weekon or before/after the specifieddate.
Examples
Days of the week follow the elixir convention of 1 being Monday and 7 being Sunday.
# Memorial Day in the US
iex> Kday.last_kday(~D[2017-05-31], 1)
~D[2017-05-29]
iex> Kday.kday_nearest(~D[2016-02-29], 2)
~D[2016-03-01]
iex> Kday.kday_on_or_after(~D[2017-11-30], 1)
~D[2017-12-04]
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jc00ke
This is great, thanks for extracting! I wrote my own
date_of_nth_weekday/3a few years ago but it depends onTimexwhich is fine for that project but not ideal for others.annad
Thank you so much for this post! I would have never known about Kday without this. I saw another post by you where you advised the person to avoid writing his own time functionality and instead utilize functions in DateTime, Timex and Calendar because they are very well tested and accommodate things like leap year. It took me awhile to understand all of those modules but it was well worth it. Thank you for all your posts!