Skip to content

A platform for community discussion. Free, open, simple.

FIX: Formkit calendar date setting back one day (#33238)

Mark VanLandingham5e7933fa3476
diff --git a/app/assets/javascripts/discourse/app/form-kit/components/fk/control/calendar.gjs b/app/assets/javascripts/discourse/app/form-kit/components/fk/control/calendar.gjs
index 63f2942d541..3e71e9ce304 100644
--- a/app/assets/javascripts/discourse/app/form-kit/components/fk/control/calendar.gjs
+++ b/app/assets/javascripts/discourse/app/form-kit/components/fk/control/calendar.gjs
@@ -34,7 +34,10 @@ export default class FKControlCalendar extends Component {
@action
setDate(date) {
- const updatedDate = new Date(date);
+ let [year, month, day] = date.split("-").map(Number);
+ month -= 1;
+
+ const updatedDate = new Date(year, month, day);
const currentDate = this.args.field.value || new Date();
updatedDate.setHours(
diff --git a/app/assets/javascripts/discourse/app/form-kit/lib/validation-parser.js b/app/assets/javascripts/discourse/app/form-kit/lib/validation-parser.js
index 6022fd07e88..cce884eea66 100644
--- a/app/assets/javascripts/discourse/app/form-kit/lib/validation-parser.js
+++ b/app/assets/javascripts/discourse/app/form-kit/lib/validation-parser.js
@@ -20,16 +20,23 @@ export default class ValidationParser {
dateBeforeOrEqualRule(input) {
return {
- date: new Date(input),
+ date: this.parseDateString(input),
};
}
dateAfterOrEqualRule(input) {
return {
- date: new Date(input),
+ date: this.parseDateString(input),
};
}
+ parseDateString(input) {
+ let [year, month, day] = input.split("-").map(Number);
+ month -= 1;
+
+ return new Date(year, month, day);
+ }
+
requiredRule(args = "") {
const [option] = args.split(",");
return {
diff --git a/app/assets/javascripts/discourse/tests/integration/components/form-kit/controls/calendar-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/form-kit/controls/calendar-test.gjs
index 7feae295e59..af13f1f77bd 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/form-kit/controls/calendar-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/form-kit/controls/calendar-test.gjs
@@ -3,6 +3,7 @@ import { module, test } from "qunit";
import Form from "discourse/components/form";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import formKit from "discourse/tests/helpers/form-kit-helper";
+import { fakeTime } from "discourse/tests/helpers/qunit-helpers";
import { i18n } from "discourse-i18n";
module(
@@ -10,6 +11,15 @@ module(
function (hooks) {
setupRenderingTest(hooks);
+ let clock = null;
+ hooks.beforeEach(() => {
+ clock = fakeTime("2025-05-03T08:00:00", "America/Chicago", true);
+ });
+
+ hooks.afterEach(() => {
+ clock.restore();
+ });
+
test("default", async function (assert) {
let data = { foo: null };
const mutateData = (x) => (data = x);