diff --git a/packages/survey-core/src/functionsfactory.ts b/packages/survey-core/src/functionsfactory.ts index 4660a6a6d9..6982f2affb 100644 --- a/packages/survey-core/src/functionsfactory.ts +++ b/packages/survey-core/src/functionsfactory.ts @@ -266,6 +266,25 @@ function dateDiff(params: any[]): any { } FunctionFactory.Instance.register("dateDiff", dateDiff); +function dateAdd(params: any[]): any { + if(!Array.isArray(params) || params.length < 2 || !params[0] || !params[1]) return null; + const date = createDate("function-dateAdd", params[0]); + const valToAdd = params[1]; + const interval = params[2] || "days"; + if (interval === "days") { + date.setDate(date.getDate() + valToAdd); + } + if (interval === "months") { + date.setMonth(date.getMonth() + valToAdd); + } + if (interval === "years") { + date.setFullYear(date.getFullYear() + valToAdd); + } + return date; +} + +FunctionFactory.Instance.register("dateAdd", dateAdd); + function isContainerReadyCore(container: any): boolean { if (!container) return false; var questions = container.questions; diff --git a/packages/survey-core/tests/expressions/expressionParserTest.ts b/packages/survey-core/tests/expressions/expressionParserTest.ts index 98a743cb4f..06ea04d672 100644 --- a/packages/survey-core/tests/expressions/expressionParserTest.ts +++ b/packages/survey-core/tests/expressions/expressionParserTest.ts @@ -370,6 +370,36 @@ QUnit.test("Run dateDiff by days", function(assert) { (values).d1 = undefined; assert.equal(runner.run(values), null, "a value is undefined"); }); +QUnit.test("Run dateAdd() for days", function(assert) { + const d1 = new Date("2021-01-01"); + const values = { d1: d1 }; + var runner = new ExpressionRunner("dateAdd({d1}, 32)"); + assert.deepEqual(runner.run(values), new Date("2021-02-02"), "February 2, 2021"); + runner = new ExpressionRunner("dateAdd({d1}, -10)"); + assert.deepEqual(runner.run(values), new Date("2020-12-22"), "December 22, 2020"); + (values).d1 = undefined; + assert.equal(runner.run(values), null, "a value is undefined"); +}); +QUnit.test("Run dateAdd() for months", function(assert) { + const d1 = new Date("2021-01-01"); + const values = { d1: d1 }; + var runner = new ExpressionRunner("dateAdd({d1}, 13, 'months')"); + assert.deepEqual(runner.run(values), new Date("2022-02-01"), "February 1, 2022"); + runner = new ExpressionRunner("dateAdd({d1}, -2, 'months')"); + assert.deepEqual(runner.run(values), new Date("2020-11-01"), "November 1, 2020"); + (values).d1 = undefined; + assert.equal(runner.run(values), null, "a value is undefined"); +}); +QUnit.test("Run dateAdd() for years", function(assert) { + const d1 = new Date("2020-02-29"); + const values = { d1: d1 }; + var runner = new ExpressionRunner("dateAdd({d1}, 2, 'years')"); + assert.deepEqual(runner.run(values), new Date("2022-03-01"), "March 1, 2022"); + runner = new ExpressionRunner("dateAdd({d1}, -2, 'years')"); + assert.deepEqual(runner.run(values), new Date("2018-03-01"), "March 1, 2018"); + (values).d1 = undefined; + assert.equal(runner.run(values), null, "a value is undefined"); +}); QUnit.test("Run getYear() function", function(assert) { var runner = new ExpressionRunner("getYear({birthday})"); var values = { birthday: new Date(1974, 1, 1) };