Skip to content

🕐 Elixir module for calculating the range of two datetimes and get the days/hours/minutes/seconds

License

Notifications You must be signed in to change notification settings

pr0grammr/time-ago

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TimeAgo

Module for getting the amount of days/hours/minutes/seconds since past a specific date. This module is inspired by the functionality of many social networks and forums

build & test Hex.pm Hex.pm

Installation

The package can be installed by adding time_ago to your list of dependencies in mix.exs:

def deps do
  [
    {:time_ago, "~> 1.1.0"}
  ]
end

Usage

# returns original date if amount of days are more than 7
iex> TimeAgo.from_date ~N[2019-12-10 23:00:00], ~N[2019-12-21 22:30:00]
{:date, ~N[2019-12-10 23:00:00]}

# returns days if amount of days are more than 0
iex> TimeAgo.from_date ~N[2019-12-18 12:00:00], ~N[2019-12-21 22:30:00]
{:days, 3}

# returns hours if amount of hours are more than 0
iex> TimeAgo.from_date ~N[2019-12-21 20:00:00], ~N[2019-12-21 22:30:00]
{:hours, 2}

# returns minutes if amount of minutes are more than 0
iex> TimeAgo.from_date ~N[2019-12-21 18:00:00], ~N[2019-12-21 18:30:00]
{:minutes, 30}

# returns seconds if amount of seconds are more than 0
iex> TimeAgo.from_date ~N[2019-12-21 13:00:00], ~N[2019-12-21 13:00:55]
{:seconds, 55}

Pattern Matching

case TimeAgo.from_date ~N[2019-12-21 20:00:00], ~N[2019-12-21 22:30:00] do
  {:days, days} ->
    IO.puts "#{days} days ago"
  {:hours, hours} ->
    IO.puts "#{hours} hours ago"
  {:minutes, minutes} ->
    IO.puts "#{minutes} minutes ago"
  {:seconds, seconds} ->
    IO.puts "#{seconds} seconds ago"
end

# => 2 hours ago

License

This module is published under the MIT license

About

🕐 Elixir module for calculating the range of two datetimes and get the days/hours/minutes/seconds

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages