as.duration

0th

Percentile

Change an object to a duration.

as.duration changes Interval, Period and numeric class objects to Duration objects. Numeric objects are changed to Duration objects with the seconds unit equal to the numeric value.

Keywords
manip, classes, methods, chron
Usage
as.duration(x)
Arguments
x
Object to be coerced to a duration
Details

Durations are exact time measurements, whereas periods are relative time measurements. See Period-class. The length of a period depends on when it occurs. Hence, a one to one mapping does not exist between durations and periods. When used with a period object, as.duration provides an inexact estimate of the length of the period; each time unit is assigned its most common number of seconds. A period of one month is converted to 2628000 seconds (approximately 30.42 days). This ensures that 12 months will sum to 365 days, or one normal year. For an exact transformation, first transform the period to an interval with as.interval.

as.duration.period displays the message "estimate only: convert periods to intervals for accuracy" by default. You can turn this message off by setting the global lubridate.verbose option to FALSE with options(lubridate.verbose = FALSE).

Value

A duration object

See Also

Duration-class, duration

Aliases
  • as.duration
  • as.duration,Duration-method
  • as.duration,Interval-method
  • as.duration,Period-method
  • as.duration,difftime-method
  • as.duration,logical-method
  • as.duration,numeric-method
Examples
span <- interval(ymd("2009-01-01"), ymd("2009-08-01")) #interval
# "2009-01-01 UTC--2009-08-01 UTC"
as.duration(span)
# 18316800s (~212 days)
as.duration(10) # numeric
# 10s
Documentation reproduced from package lubridate, version 1.5.6, License: GPL-2

Community examples

ankitkatiyar91 at Aug 15, 2017 lubridate v1.6.0

# Get the hours between two dates as.duration(interval("2015-01-20 09:50:34", "2015-01-20 16:37:07")) #Output "24393s (~6.78 hours)" # Get the numeric value of it as.double(as.duration(interval("2015-01-20 09:50:34", "2015-01-20 16:37:07"))) #Output 24393