Skip to contents

It calls vague_dt to do the actual formatting.

Usage

time_ago(date, format = c("default", "short", "terse"))

Arguments

date

Date(s), as.POSIXct will be called on them.

format

Format, currently available formats are: ‘default’, ‘short’, ‘terse’. See examples below.

Value

Character vector of the formatted time intervals.

Examples

now <- Sys.time()

time_ago(now)
#> [1] "moments ago"
time_ago(now - as.difftime(30, units = "secs"))
#> [1] "less than a minute ago"
time_ago(now - as.difftime(14, units = "mins"))
#> [1] "14 minutes ago"
time_ago(now - as.difftime(5, units = "hours"))
#> [1] "5 hours ago"
time_ago(now - as.difftime(25, units = "hours"))
#> [1] "a day ago"
time_ago(now - as.difftime(5, units = "days"))
#> [1] "5 days ago"
time_ago(now - as.difftime(30, units = "days"))
#> [1] "about a month ago"
time_ago(now - as.difftime(365, units = "days"))
#> [1] "about a year ago"
time_ago(now - as.difftime(365 * 10, units = "days"))
#> [1] "10 years ago"

## Short format
time_ago(format = "short", now)
#> [1] "<1 min"
time_ago(format = "short", now - as.difftime(30, units = "secs"))
#> [1] "<1 min"
time_ago(format = "short", now - as.difftime(14, units = "mins"))
#> [1] "14 min"
time_ago(format = "short", now - as.difftime(5, units = "hours"))
#> [1] "5 hours"
time_ago(format = "short", now - as.difftime(25, units = "hours"))
#> [1] "1 day"
time_ago(format = "short", now - as.difftime(5, units = "days"))
#> [1] "5 day"
time_ago(format = "short", now - as.difftime(30, units = "days"))
#> [1] "1 mon"
time_ago(format = "short", now - as.difftime(365, units = "days"))
#> [1] "1 year"
time_ago(format = "short", now - as.difftime(365 * 10, units = "days"))
#> [1] "10 years"

## Even shorter, terse format, (almost always) exactly 3 characters wide
time_ago(format = "terse", now)
#> [1] " 0s"
time_ago(format = "terse", now - as.difftime(30, units = "secs"))
#> [1] "30s"
time_ago(format = "terse", now - as.difftime(14, units = "mins"))
#> [1] "14m"
time_ago(format = "terse", now - as.difftime(5, units = "hours"))
#> [1] " 5h"
time_ago(format = "terse", now - as.difftime(25, units = "hours"))
#> [1] " 1d"
time_ago(format = "terse", now - as.difftime(5, units = "days"))
#> [1] " 5d"
time_ago(format = "terse", now - as.difftime(30, units = "days"))
#> [1] " 1M"
time_ago(format = "terse", now - as.difftime(365, units = "days"))
#> [1] " 1y"
time_ago(format = "terse", now - as.difftime(365 * 10, units = "days"))
#> [1] "10y"