Skip to contents

Convert duration to date-time Convert a string with a duration (e.g. 'an hour ago') to a date-time string, based on a reference time

Usage

duration2datetime(
  str,
  ref_time = Sys.time(),
  output_format = "%Y-%m-%d %H:%M:%S %Z"
)

Arguments

str

String with a duration (see examples)

ref_time

Reference time (default: Sys.time(), current time)

output_format

String with the format of the output (default: "%Y-%m-%d %H:%M:%S %Z")

Value

Date-time object based on the input string, str, and the reference time ref_time.

Examples

duration2datetime("a minute ago")
#> [1] "2023-12-18 15:40:17 UTC"
duration2datetime("an hour ago")
#> [1] "2023-12-18 14:41:17 UTC"
duration2datetime("a day ago")
#> [1] "2023-12-17 15:41:17 UTC"
duration2datetime("a week ago")
#> [1] "2023-12-11 15:41:17 UTC"
duration2datetime("a month ago")
#> [1] "2023-11-18 05:11:17 UTC"
duration2datetime("a year ago")
#> [1] "2022-12-18 09:41:17 UTC"
duration2datetime("2 minutes ago")
#> [1] "2023-12-18 15:39:17 UTC"
duration2datetime("2 hours ago")
#> [1] "2023-12-18 13:41:17 UTC"
duration2datetime("2 days ago")
#> [1] "2023-12-16 15:41:17 UTC"
duration2datetime("2 weeks ago")
#> [1] "2023-12-04 15:41:17 UTC"
duration2datetime("2 months ago")
#> [1] "2023-10-18 18:41:17 UTC"
duration2datetime("2 years ago")
#> [1] "2021-12-18 03:41:17 UTC"