6 Commits

2 changed files with 24 additions and 10 deletions
+2 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "@tp/tp-date-input", "name": "@tp/tp-date-input",
"version": "2.0.0", "version": "2.1.0",
"description": "", "description": "",
"main": "tp-date-input.js", "main": "tp-date-input.js",
"scripts": { "scripts": {
@@ -13,8 +13,7 @@
"author": "trading_peter", "author": "trading_peter",
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"date-fns": "^2.0.0", "luxon": "^3.0.0",
"date-fns-tz": "^2.0.0",
"lit": "^3.0.0" "lit": "^3.0.0"
} }
} }
+22 -7
View File
@@ -291,17 +291,27 @@ class TpDateInput extends EventHelpers(ControlState(FormElement(LitElement))) {
return; return;
} }
const date = parse(i0 + '-' + i1 + '-' + i2, this._inputAssign.join('-'), new Date()); // Convert luxon format to match input assignment
const luxonFormat = this._inputAssign.map(part => {
switch (part) {
case 'MM': return 'LL';
case 'dd': return 'dd';
case 'y': return 'yyyy';
default: return part;
}
}).join('-');
// Parse the date in the specified timezone (or local if not set).
// This interprets the entered values as being in that timezone.
const dt = DateTime.fromFormat(i0 + '-' + i1 + '-' + i2, luxonFormat, { zone: this.timeZone || 'local' });
if (dt.isValid) { if (dt.isValid) {
this.inputs[0].invalid = false; this.inputs[0].invalid = false;
this.inputs[1].invalid = false; this.inputs[1].invalid = false;
this.inputs[2].invalid = false; this.inputs[2].invalid = false;
// Convert to specified timezone or use system timezone this.date = dt.toJSDate();
const finalDt = this.timeZone ? dt.setZone(this.timeZone) : dt; this.value = dt.toUTC().toISO();
this.date = finalDt.toJSDate();
this.value = finalDt.toISO();
this.invalid = false; this.invalid = false;
this.dispatchEvent(new CustomEvent('value-changed', { detail: this.value, bubbles: true, composed: true })); this.dispatchEvent(new CustomEvent('value-changed', { detail: this.value, bubbles: true, composed: true }));
} else { } else {
@@ -455,8 +465,13 @@ class TpDateInput extends EventHelpers(ControlState(FormElement(LitElement))) {
return; return;
} }
const dt = this._toDateTime(this.value); let dt = this._toDateTime(this.value);
if (dt && dt.isValid) { if (dt && dt.isValid) {
// Convert to user's timezone for display (input values are expected to be UTC)
if (this.timeZone) {
dt = dt.setZone(this.timeZone);
}
// Convert luxon format parts to match display format // Convert luxon format parts to match display format
this._input0 = dt.toFormat(this._getLuxonFormat(this._inputAssign[0])); this._input0 = dt.toFormat(this._getLuxonFormat(this._inputAssign[0]));
this._input1 = dt.toFormat(this._getLuxonFormat(this._inputAssign[1])); this._input1 = dt.toFormat(this._getLuxonFormat(this._inputAssign[1]));
@@ -512,4 +527,4 @@ class TpDateInput extends EventHelpers(ControlState(FormElement(LitElement))) {
} }
} }
window.customElements.define('tp-date-input', TpDateInput); window.customElements.define('tp-date-input', TpDateInput);