Skip to main content

timestamp_add

Gets the difference between the timestamps in the specified units by truncating the fraction part.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.timestamp_add(unit=<unit>, quantity=<quantity>, ts=<ts>)

Parameters

Parameter

Type

Description

unit

literal string

This indicates the units of the difference between the given timestamps. Supported options are (case insensitive): "YEAR", "QUARTER", "MONTH", "WEEK", "DAY", "HOUR", "MINUTE", "SECOND", "MILLISECOND" and "MICROSECOND".

quantity

pyspark.sql.Column or str

The number of units of time that you want to add.

ts

pyspark.sql.Column or str

A timestamp to which you want to add.

Returns

pyspark.sql.Column: the difference between the timestamps.

Examples

Python
import datetime
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame(
[(datetime.datetime(2016, 3, 11, 9, 0, 7), 2),
(datetime.datetime(2024, 4, 2, 9, 0, 7), 3)], ['ts', 'quantity'])
df.select('*', dbf.timestamp_add('year', 'quantity', 'ts')).show()
df.select('*', dbf.timestamp_add('WEEK', dbf.lit(5), df.ts)).show()
df.select('*', dbf.timestamp_add('day', dbf.lit(-5), 'ts')).show()