Description Usage Arguments Details Note See Also Examples
Date time arithmetic functions defined for Column
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44  add_months(y, x)
datediff(y, x)
date_add(y, x)
date_format(y, x)
date_sub(y, x)
from_utc_timestamp(y, x)
months_between(y, x, ...)
next_day(y, x)
to_utc_timestamp(y, x)
## S4 method for signature 'Column'
datediff(y, x)
## S4 method for signature 'Column'
months_between(y, x, roundOff = NULL)
## S4 method for signature 'Column,character'
date_format(y, x)
## S4 method for signature 'Column,character'
from_utc_timestamp(y, x)
## S4 method for signature 'Column,character'
next_day(y, x)
## S4 method for signature 'Column,character'
to_utc_timestamp(y, x)
## S4 method for signature 'Column,numeric'
add_months(y, x)
## S4 method for signature 'Column,numeric'
date_add(y, x)
## S4 method for signature 'Column,numeric'
date_sub(y, x)

y 
Column to compute on. 
x 
For class

... 
additional argument(s).

roundOff 
an optional parameter to specify if the result is rounded off to 8 digits 
datediff
: Returns the number of days from y
to x
.
If y
is later than x
then the result is positive.
months_between
: Returns number of months between dates y
and x
.
If y
is later than x
, then the result is positive. If y
and x
are on the same day of month, or both are the last day of month, time of day will be ignored.
Otherwise, the difference is calculated based on 31 days per month, and rounded to 8 digits.
date_format
: Converts a date/timestamp/string to a value of string in the format
specified by the date format given by the second argument. A pattern could be for instance
dd.MM.yyyy
and could return a string like '18.03.1993'. All
pattern letters of java.time.format.DateTimeFormatter
can be used.
Note: Use when ever possible specialized functions like year
. These benefit from a
specialized implementation.
from_utc_timestamp
: This is a common function for databases supporting TIMESTAMP WITHOUT
TIMEZONE. This function takes a timestamp which is timezoneagnostic, and interprets it as a
timestamp in UTC, and renders that timestamp as a timestamp in the given time zone.
However, timestamp in Spark represents number of microseconds from the Unix epoch, which is not
timezoneagnostic. So in Spark this function just shift the timestamp value from UTC timezone to
the given timezone.
This function may return confusing result if the input is a string with timezone, e.g.
(20180313T06:18:23+00:00
). The reason is that, Spark firstly cast the string to
timestamp according to the timezone in the string, and finally display the result by converting
the timestamp to string according to the session local timezone.
next_day
: Given a date column, returns the first date which is later than the value of
the date column that is on the specified day of the week. For example,
next_day("20150727", "Sunday")
returns 20150802 because that is the first Sunday
after 20150727. Day of the week parameter is case insensitive, and accepts first three or
two characters: "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun".
to_utc_timestamp
: This is a common function for databases supporting TIMESTAMP WITHOUT
TIMEZONE. This function takes a timestamp which is timezoneagnostic, and interprets it as a
timestamp in the given timezone, and renders that timestamp as a timestamp in UTC.
However, timestamp in Spark represents number of microseconds from the Unix epoch, which is not
timezoneagnostic. So in Spark this function just shift the timestamp value from the given
timezone to UTC timezone.
This function may return confusing result if the input is a string with timezone, e.g.
(20180313T06:18:23+00:00
). The reason is that, Spark firstly cast the string to
timestamp according to the timezone in the string, and finally display the result by converting
the timestamp to string according to the session local timezone.
add_months
: Returns the date that is numMonths (x
) after startDate (y
).
date_add
: Returns the date that is x
days after.
date_sub
: Returns the date that is x
days before.
datediff since 1.5.0
months_between since 1.5.0
date_format since 1.5.0
from_utc_timestamp since 1.5.0
next_day since 1.5.0
to_utc_timestamp since 1.5.0
add_months since 1.5.0
date_add since 1.5.0
date_sub since 1.5.0
Other data time functions:
timestamp_seconds()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  ## Not run:
dts < c("20050102 18:47:22",
"20051224 16:30:58",
"20051028 07:30:05",
"20051228 07:01:05",
"20060124 00:01:10")
y < c(2.0, 2.2, 3.4, 2.5, 1.8)
df < createDataFrame(data.frame(time = as.POSIXct(dts), y = y))
## End(Not run)
## Not run:
tmp < createDataFrame(data.frame(time_string1 = as.POSIXct(dts),
time_string2 = as.POSIXct(dts[order(runif(length(dts)))])))
tmp2 < mutate(tmp, datediff = datediff(tmp$time_string1, tmp$time_string2),
monthdiff = months_between(tmp$time_string1, tmp$time_string2))
head(tmp2)
## End(Not run)
## Not run:
tmp < mutate(df, from_utc = from_utc_timestamp(df$time, "PST"),
to_utc = to_utc_timestamp(df$time, "PST"))
head(tmp)
## End(Not run)
## Not run:
tmp < mutate(df, t1 = add_months(df$time, 1),
t2 = date_add(df$time, 2),
t3 = date_sub(df$time, 3),
t4 = next_day(df$time, "Sun"))
head(tmp)
## End(Not run)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.