Skip to main content Skip to complementary content

addmonths - script and chart function

This function returns the date occurring n months after startdate or, if n is negative, the date occurring n months before startdate.

Syntax:  

AddMonths(startdate, n , [ , mode])

Return data type: dual

The addmonths() function adds or subtracts a defined number of months, n, from a startdate and returns the resultant date.

The mode argument will impact startdate values on or after the 28th of the month. By setting the mode argument to 1, the addmonths() function returns a date that is equal in relative distance to the end of the month as the startdate.

Example diagram of addmonths() function

Example diagram showing how the various components of the addmonths function come together to produce an output date.

For example, February 28 is the last day of the month. If the addmonths() function, with a mode of 1, is used to return the date two months later, the function will return the last date of April, April 30.

Example diagram of addmonths() function, with mode=1

Example diagram showing how the 'mode' argument can be altered to change the output date of the addmonths function.
Arguments
Argument Description
startdate The start date as a time stamp, for example '2012-10-12'.
n Number of months as a positive or negative integer.
mode Specifies if the month is added relative to the beginning or to the end of the month. Default mode is 0 for additions relative to the beginning of the month. Set mode to 1 for additions relative to the end of the month. When mode is set to 1 and the input date is the 28th or above, the function checks how many days are left to reach the end of the month on the startdate. The same number of days to reach the end of the month are set on the date returned.

When to use it

The addmonths() function will commonly be used in an expression to find a date a given number of months before or after a period of time.

For example, the addmonths() function can be used to identify the end date of mobile phone contracts.

Function examples
Example Result
addmonths ('01/29/2003' ,3) Returns '04/29/2003'.
addmonths ('01/29/2003',3,0) Returns '04/29/2003'.
addmonths ('01/29/2003',3,1) Returns '04/28/2003'.
addmonths ('01/29/2003',1,0) Returns '02/28/2003'.
addmonths ('01/29/2003',1,1) Returns '02/26/2003'.
addmonths ('02/28/2003',1,0) Returns '03/28/2003'.
addmonths ('02/28/2003',1,1) Returns '03/31/2003'.
addmonths ('01/29/2003',-3) Returns '10/29/2002'.

Regional settings

Unless otherwise specified, the examples in this topic use the following date format: MM/DD/YYYY. The date format is specified in the SET DateFormat statement in your data load script. The default date formatting may be different in your system, due to your regional settings and other factors. You can change the formats in the examples below to suit your requirements. Or you can change the formats in your load script to match these examples.

Default regional settings in apps are based on the regional system settings of the computer or server where Qlik Sense is installed. If the Qlik Sense server you are accessing is set to Sweden, the Data load editor will use Swedish regional settings for dates, time, and currency. These regional format settings are not related to the language displayed in the Qlik Sense user interface. Qlik Sense will be displayed in the same language as the browser you are using.

Example 1 – No additional arguments

Example 2 – Relative month end

Example 3– Chart object example

Example 4 – Scenario

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!