From 87e3d6c81b56df4b22f3bec090e91162e8cb86a2 Mon Sep 17 00:00:00 2001 From: Tsanie Date: Wed, 27 Mar 2024 02:30:04 +0800 Subject: [PATCH] fix: date UTC conversion issue fix: `type.create` index offset issue --- lib/ui/date.js | 39 +++++++++++++++++---------------------- lib/ui/grid/column.js | 5 ++++- lib/ui/grid/grid.js | 2 +- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/ui/date.js b/lib/ui/date.js index a0a2a3e..f8c5bf1 100644 --- a/lib/ui/date.js +++ b/lib/ui/date.js @@ -32,26 +32,17 @@ export function createDateInput(min, max, element) { /** * 将日期转换为 `yyyy-MM-dd` 格式的字符串 * @param {Date} dt 要转换的日期值 + * @param {boolean} [utc] 是否采用 UTC 值 * @returns 返回 `yyyy-MM-dd` 格式的字符串 */ -export function toDateValue(dt) { +export function toDateValue(dt, utc) { if (isNaN(dt)) { return ''; } - const month = String(dt.getMonth() + 1).padStart(2, '0'); - const date = String(dt.getDate()).padStart(2, '0'); - return `${dt.getFullYear()}-${month}-${date}`; -} - -function resolveDate(s) { - if (s instanceof Date) { - return s; - } - const ticks = Number(s); - if (!isNaN(ticks) && ticks > 0) { - return new Date((ticks - 621355968e9) / 1e4); - } - return new Date(s); + const year = utc ? dt.getUTCFullYear() : dt.getFullYear(); + const month = String((utc ? dt.getUTCMonth() : dt.getMonth()) + 1).padStart(2, '0'); + const date = String(utc ? dt.getUTCDate() : dt.getDate()).padStart(2, '0'); + return `${year}-${month}-${date}`; } /** @@ -66,11 +57,15 @@ function resolveDate(s) { * @returns {string} 返回格式化后的日期字符串 */ export function formatDate(date) { - date = resolveDate(date); - if (date instanceof Date && !isNaN(date)) { + if (date instanceof Date) { return `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`; } - return ''; + const ticks = Number(date); + if (!isNaN(ticks) && ticks > 0) { + date = new Date((ticks - 621355968e9) / 1e4); + return `${date.getUTCMonth() + 1}/${date.getUTCDate()}/${date.getUTCFullYear()}`; + } + return date; } /** @@ -94,7 +89,7 @@ export function setDateValue(element, val) { if (!(val instanceof Date)) { val = new Date((val - 621355968e9) / 1e4); } - element.value = toDateValue(val); + element.value = toDateValue(val, true); } } else { element.innerText = formatDate(val); @@ -117,7 +112,7 @@ export function setDateValue(element, val) { export function getDateValue(element, formatter) { const date = element?.valueAsDate; if (date instanceof Date && !isNaN(date)) { - const year = date.getFullYear(); + const year = date.getUTCFullYear(); if (year < 1900 || year > 9999) { return ''; } @@ -162,7 +157,7 @@ export class DateSelector { * minDate: '2020-01-01', * maxDate: '2024-02-01', * valueFormatter: (date) => { - * return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`; // 2024/1/31 + * return `${date.getUTCFullYear()}/${date.getUTCMonth() + 1}/${date.getUTCDate()}`; // 2024/1/31 * } * }; * const dateSelectorFrom = new DateSelector(opts); @@ -248,7 +243,7 @@ export class DateSelector { _getDate(date) { if (date instanceof Date && !isNaN(date)) { - const year = date.getFullYear(); + const year = date.getUTCFullYear(); if (year < 1900 || year > 9999) { return null; } diff --git a/lib/ui/grid/column.js b/lib/ui/grid/column.js index be85078..a15fe71 100644 --- a/lib/ui/grid/column.js +++ b/lib/ui/grid/column.js @@ -768,7 +768,10 @@ export class GridDateColumn extends GridColumn { * @returns {string} */ static getValue(e, col) { - return getDateValue(e.target, col.dateValueFormatter); + if (e.target.tagName === 'INPUT') { + return getDateValue(e.target, col.dateValueFormatter); + } + return e.target.innerText; } /** diff --git a/lib/ui/grid/grid.js b/lib/ui/grid/grid.js index 823d0c0..b78b278 100644 --- a/lib/ui/grid/grid.js +++ b/lib/ui/grid/grid.js @@ -2652,7 +2652,7 @@ export class Grid { if (!readonly && GridColumnTypeEnum.isAlwaysEditing(col.type)) { element = type.createEdit(e => this._onRowChanged(e, exists + i, col, e.target.checked, cell), col, exists + i); } else { - element = type.create(col, i, this); + element = type.create(col, exists + i, this); if (typeof col.class === 'string') { type.setClass(element, col.class); }