Articles in this section

How to subtract days, weeks, months, quarters or years from a selected date using DateSubtract()

About this article

This article explains what the DateSubtract() function is and provide examples of how it can be used with the binding syntax in smart templates.  

 

Prerequisites

 

 

What is the DateSubtract() function? 

DateSubtract() is a function that calculates a date based on another date. It subtracts days, weeks, months, quarters or years from the source date.

 

DateSubtract() logic

Syntax {{DateSubtract(Date, Number, Text)}}
Input DateTime, Units, UnitType
Output Date with Units added, where Units are UnitType ("d", "M", "Q", "w", "y")

 

 
  • The values "d", "M", "Q", "w" and "y" are case sensitive.

 

The syntax below can also be used. If the 'Text' variable is omitted then the Units are interpreted as days.

Syntax {{DateSubtract(Date, Number)}}
Input DateTime, Units
Output Date with Units subtracted, where Units are Days ("d")

 

DateSubtract() function examples

Example 1

Using DateSubtract() to subtract 14 days/weeks/months/quarters/years based on the value of the "Date" question originating from the response form. In this example, the value of Date is "1 January 2022".

Description Binding Output
Date - 14 days {{DateSubtract(Form.Date, 14)}}

2021-12-18

Date - 14 days {{DateSubtract(Form.Date, 14, "d")}}

2021-12-18

Date - 14 weeks {{DateSubtract(Form.Date, 14, "w")}}

2021-09-25

Date - 14 months {{DateSubtract(Form.Date, 14, "M")}}

2020-11-01

Date - 14 quarters {{DateSubtract(Form.Date, 14, "Q")}}

2018-07-01

Date - 14 years {{DateSubtract(Form.Date, 14, "y")}}

2008-01-01

 

Example 2

Using DateSubtract() function together with FormatDateTime() function to format the output.
In this example, the value of Date is "1 January 2022".

Description Binding Output
Date - 14 days {{FormatDateTime(DateSubtract(Form.Date, 14, "d"), "d MMMM yyyy", "en-US")}}

18 December 2021

 

Example 3

When the year is a leap year, note that it makes a difference if you use a binding 'Date - 365 days' or 'Date - 1 year'.

Current date Binding Output
2024-02-29 {{DateSubtract(Form.Date, 365, "d")}}

2023-03-01

2024-02-29 {{DateSubtract(Form.Date, 1, "y")}}

2023-02-28

2024-03-01 {{DateSubtract(Form.Date, 365, "d")}}

2023-03-02

2024-03-01 {{DateSubtract(Form.Date, 1, "y")}}

2023-03-01

 

Related articles

 

 

user profile workbook tech_role
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.