Skip to content

Commit

Permalink
Implement the dateAdd function (#9196)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanTsukanov authored Dec 23, 2024
1 parent a930ab8 commit 665a745
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/survey-core/src/functionsfactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions packages/survey-core/tests/expressions/expressionParserTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,36 @@ QUnit.test("Run dateDiff by days", function(assert) {
(<any>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");
(<any>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");
(<any>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");
(<any>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) };
Expand Down

0 comments on commit 665a745

Please sign in to comment.