[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  difftime: Time Intervals / Differences

##  Description

Time intervals creation, printing, and some arithmetic. The ` [ print
](/link/print?package=base&version=3.6.2) () ` method calls these “time
differences”.

##  Usage

    
    
    time1 - time2
    
    difftime(time1, time2, tz,
             units = c("auto", "secs", "mins", "hours",
                       "days", "weeks"))
    
    as.difftime(tim, format = "%X", units = "auto")
    
    # S3 method for difftime
    format(x, ...)
    # S3 method for difftime
    units(x)
    # S3 method for difftime
    units(x) <- value
    # S3 method for difftime
    as.double(x, units = "auto", ...)
    
    ## Group methods, notably for round(), signif(), floor(),
    ## ceiling(), trunc(), abs(); called directly, *not* as Math():
    # S3 method for difftime
    Math(x, …)

##  Arguments

time1, time2

[ date-time ](/link/date-time?package=base&version=3.6.2) or [ date
](/link/date?package=base&version=3.6.2&to=%3DDates) objects.

tz

an optional [ time zone ](/link/time%20zone?package=base&version=3.6.2)
specification to be used for the conversion, mainly for ` "POSIXlt" ` objects.

units

character string. Units in which the results are desired. Can be abbreviated.

value

character string. Like ` units ` , except that abbreviations are not allowed.

tim

character string or numeric value specifying a time interval.

format

character specifying the format of ` tim ` : see ` [ strptime
](/link/strptime?package=base&version=3.6.2) ` . The default is a locale-
specific time format.

x

an object inheriting from class ` "difftime" ` .

…

arguments to be passed to or from other methods.

##  Details

Function ` difftime ` calculates a difference of two date/time objects and
returns an object of class ` "difftime" ` with an attribute indicating the
units. The ` [ Math
](/link/Math?package=base&version=3.6.2&to=%3DS3groupGeneric) ` group method
provides ` [ round ](/link/round?package=base&version=3.6.2) ` , ` [ signif
](/link/signif?package=base&version=3.6.2) ` , ` [ floor
](/link/floor?package=base&version=3.6.2) ` , ` [ ceiling
](/link/ceiling?package=base&version=3.6.2) ` , ` [ trunc
](/link/trunc?package=base&version=3.6.2) ` , ` [ abs
](/link/abs?package=base&version=3.6.2) ` , and ` [ sign
](/link/sign?package=base&version=3.6.2) ` methods for objects of this class,
and there are methods for the group-generic (see ` [ Ops
](/link/Ops?package=base&version=3.6.2&to=%3DS3groupGeneric) ` ) logical and
arithmetic operations.

If ` units = "auto" ` , a suitable set of units is chosen, the largest
possible (excluding ` "weeks" ` ) in which all the absolute differences are
greater than one.

Subtraction of date-time objects gives an object of this class, by calling `
difftime ` with ` units = "auto" ` . Alternatively, ` as.difftime() ` works on
character-coded or numeric time intervals; in the latter case, units must be
specified, and ` format ` has no effect.

Limited arithmetic is available on ` "difftime" ` objects: they can be added
or subtracted, and multiplied or divided by a numeric vector. In addition,
adding or subtracting a numeric vector by a ` "difftime" ` object implicitly
converts the numeric vector to a ` "difftime" ` object with the same units as
the ` "difftime" ` object. There are methods for ` [ mean
](/link/mean?package=base&version=3.6.2) ` and ` [ sum
](/link/sum?package=base&version=3.6.2) ` (via the ` [ Summary
](/link/Summary?package=base&version=3.6.2&to=%3DS3groupGeneric) ` group
generic), and ` [ diff ](/link/diff?package=base&version=3.6.2) ` via ` [
diff.default ](/link/diff.default?package=base&version=3.6.2) ` building on
the ` "difftime" ` method for arithmetic, notably ` - ` .

The units of a ` "difftime" ` object can be ex