Table of Contents

Class DateAdd

Namespace
YndigoBlue.Velocity.Functions
Assembly
YndigoBlue.Velocity.dll

Represents a date/time arithmetic function that adds or subtracts a number of date/time units to a date, datetime, or timestamp value.

public class DateAdd : Function, ICheckItem, IDefaultItem, IFilterItem, IElement
Inheritance
DateAdd
Implements

Remarks

DateAdd(DatePart, amount, date) → date shifted by amount units.

The datePart argument specifies the unit of time to add (e.g. Year, Month, Day, Hour, Minute, Second). Negative values for amount perform subtraction.

The SQL generated is database-specific and is handled by each adaptor's BuildFunction override.

Examples

Calculate the expiry date by adding 30 days to each subscription start date:

var schema = manager.LoadSchema("billing");
var subscriptions = schema["subscriptions"];

var query = new Query()
    .Select([ subscriptions["id"], new Expression("expiry_date", new DateAdd(DatePart.Day, new Literal<int>(30), subscriptions["start_date"])) ])
    .From(subscriptions);

var results = manager.Retrieve(query);

Use the typed integer + column constructor to add a fixed number of months to a date column:

var schema = manager.LoadSchema("billing");
var subscriptions = schema["subscriptions"];
Column startDateColumn = subscriptions["start_date"];

// Add 12 months to each start date
var query = new Query()
    .Select([ subscriptions["id"], new Expression("renewal_date", new DateAdd(DatePart.Month, 12, startDateColumn)) ])
    .From(subscriptions);

var results = manager.Retrieve(query);

Use the column + column constructor to add a variable number of days (stored in another column) to a date:

var schema = manager.LoadSchema("projects");
var tasks = schema["tasks"];
Column startColumn = tasks["start_date"];
Column durationColumn = tasks["duration_days"];

var query = new Query()
    .Select([ tasks["id"], new Expression("end_date", new DateAdd(DatePart.Day, durationColumn, startColumn)) ])
    .From(tasks);

var results = manager.Retrieve(query);

Add a fixed number of days to a hard-coded DateOnly literal:

var referenceDate = new DateOnly(2024, 1, 1);

// Add 90 days to a literal date
var expr = new DateAdd(DatePart.Day, 90, referenceDate);

Constructors

DateAdd(DatePart, int, DateOnly)

Initializes a new instance of DateAdd with an integer amount and a DateOnly literal.

DateAdd(DatePart, int, DateTime)

Initializes a new instance of DateAdd with an integer amount and a DateTime literal.

DateAdd(DatePart, int, Column)

Initializes a new instance of DateAdd with an integer amount and a date column.

DateAdd(DatePart, IElement, IElement)

Initializes a new instance of DateAdd.

DateAdd(DatePart, Column, DateOnly)

Initializes a new instance of DateAdd with a column amount and a DateOnly literal.

DateAdd(DatePart, Column, DateTime)

Initializes a new instance of DateAdd with a column amount and a DateTime literal.

DateAdd(DatePart, Column, Column)

Initializes a new instance of DateAdd with a column for both amount and date.

Properties

DatePart

Gets the date/time component unit used for the addition.