# SPL: Date, Time and Datetime Handling SPL offers a wealth of functions for date, time and datetime handling, including calculating the current time/date, extracting a component from a date/time/datetime, combining components to generate a date/time/datetime, format conversion, precision tuning, getting a related date, calculating the time interval, calculating the relative date, equivalence comparison, workday-related calculations, evenly spaced time series handling, etc. These functions can be divided into two categories – basic handling and computations.

## 1. Basic handling

### 1.2 Extracting component

Extract the year, month, date, hour, minute, second and week from date, time or datetime objects or strings.

In the following table, dt is a datetime object, such as datetime("2018-08-15 16:07:58:327","yyyy-MM-dd HH🇲🇲ss:SSS").

### 1.3 Combining components

Generate a date, time or datetime object using a year value, a month value, an hour value, a minute value, a second value, a long integer, or a datetime string.

In the following table, dt is a datetime object, such as datetime("2018-08-15 16:07:58:327","yyyy-MM-dd HH🇲🇲ss:SSS").

### 1.4 Conversion

In the following table, d is a date object, such as date("2018-08-15");

t is a time object, such as time("16:07:58:327","HH🇲🇲ss:SSS");

dt is datetime object, such as datetime("2018-08-15 16:07:58:327","yyyy-MM-dd HH🇲🇲ss:SSS").

## 2. Computations

### 2.1 Calculating a related date

In the following table, dt is a date object, such as date("2020-04-16").

### 2.2 Calculating the time interval

Calculate the time interval between two time points.

In the following table, dt1 and dt2 are datetime("2008-08-08 20:00:00") and datetime("2018-05-28 10:27:15") respectively.

### 2.3 Calculating a relative datetime

elapse(dt,k)

The function calculates a new datetime when a specified number of days, years, quarters, months, seconds or milliseconds k is added to or subtracted from a given datetime dt. The new date is in the future when k is a positive number and in the past when it is a negative number. If the original datetime is in the last day a month, the function, by default, will adjust the result datetime to the last day of the current month.

In the following table, dt is datetime("2017-02-28 10:27:15"), which is in the last day of February.

### 2.5 Workday-related calculations

workday(dt,k,h)  Calculates the workday k days after the given date dt.

workdays(dt1,dt2,h)  Returns a sequence of workdays between date dt1 and date dt2.

h is a sequence of holidays or non-holidays, whose member (members) is (are) treated as a holiday (holidays) if they are not weekends, and as a non-holiday (non-holidays) if they are weekends.

### 2.6 Evenly-spaced time series

periods(s,e,i)

s and e are time variables; i is an integer. The function returns a sequence of dates/times that are a certain time period (i) apart between two given time points s (inclusive) and e (inclusive). The default unit of i is day, and its default value is 1. We can use @x option in the function to exclude the endpoints.

range(s,e,k:n)

Both s and e are time variables. The function divides the time interval between s and e evenly into n segments, and returns the time range of the kth segment. The result’s degree of precision is determined by the data type of s and e. Accurate to day when they are date type, and to second when they are datetime type.

Find more date/time/datetime operations in date(), time(), and datetime() functions in SPL Function Reference.