fix: date UTC conversion issue
fix: `type.create` index offset issue
This commit is contained in:
parent
fd8899c597
commit
87e3d6c81b
@ -32,26 +32,17 @@ export function createDateInput(min, max, element) {
|
|||||||
/**
|
/**
|
||||||
* 将日期转换为 `yyyy-MM-dd` 格式的字符串
|
* 将日期转换为 `yyyy-MM-dd` 格式的字符串
|
||||||
* @param {Date} dt 要转换的日期值
|
* @param {Date} dt 要转换的日期值
|
||||||
|
* @param {boolean} [utc] 是否采用 UTC 值
|
||||||
* @returns 返回 `yyyy-MM-dd` 格式的字符串
|
* @returns 返回 `yyyy-MM-dd` 格式的字符串
|
||||||
*/
|
*/
|
||||||
export function toDateValue(dt) {
|
export function toDateValue(dt, utc) {
|
||||||
if (isNaN(dt)) {
|
if (isNaN(dt)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
const month = String(dt.getMonth() + 1).padStart(2, '0');
|
const year = utc ? dt.getUTCFullYear() : dt.getFullYear();
|
||||||
const date = String(dt.getDate()).padStart(2, '0');
|
const month = String((utc ? dt.getUTCMonth() : dt.getMonth()) + 1).padStart(2, '0');
|
||||||
return `${dt.getFullYear()}-${month}-${date}`;
|
const date = String(utc ? dt.getUTCDate() : dt.getDate()).padStart(2, '0');
|
||||||
}
|
return `${year}-${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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,11 +57,15 @@ function resolveDate(s) {
|
|||||||
* @returns {string} 返回格式化后的日期字符串
|
* @returns {string} 返回格式化后的日期字符串
|
||||||
*/
|
*/
|
||||||
export function formatDate(date) {
|
export function formatDate(date) {
|
||||||
date = resolveDate(date);
|
if (date instanceof Date) {
|
||||||
if (date instanceof Date && !isNaN(date)) {
|
|
||||||
return `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`;
|
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)) {
|
if (!(val instanceof Date)) {
|
||||||
val = new Date((val - 621355968e9) / 1e4);
|
val = new Date((val - 621355968e9) / 1e4);
|
||||||
}
|
}
|
||||||
element.value = toDateValue(val);
|
element.value = toDateValue(val, true);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
element.innerText = formatDate(val);
|
element.innerText = formatDate(val);
|
||||||
@ -117,7 +112,7 @@ export function setDateValue(element, val) {
|
|||||||
export function getDateValue(element, formatter) {
|
export function getDateValue(element, formatter) {
|
||||||
const date = element?.valueAsDate;
|
const date = element?.valueAsDate;
|
||||||
if (date instanceof Date && !isNaN(date)) {
|
if (date instanceof Date && !isNaN(date)) {
|
||||||
const year = date.getFullYear();
|
const year = date.getUTCFullYear();
|
||||||
if (year < 1900 || year > 9999) {
|
if (year < 1900 || year > 9999) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@ -162,7 +157,7 @@ export class DateSelector {
|
|||||||
* minDate: '2020-01-01',
|
* minDate: '2020-01-01',
|
||||||
* maxDate: '2024-02-01',
|
* maxDate: '2024-02-01',
|
||||||
* valueFormatter: (date) => {
|
* 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);
|
* const dateSelectorFrom = new DateSelector(opts);
|
||||||
@ -248,7 +243,7 @@ export class DateSelector {
|
|||||||
|
|
||||||
_getDate(date) {
|
_getDate(date) {
|
||||||
if (date instanceof Date && !isNaN(date)) {
|
if (date instanceof Date && !isNaN(date)) {
|
||||||
const year = date.getFullYear();
|
const year = date.getUTCFullYear();
|
||||||
if (year < 1900 || year > 9999) {
|
if (year < 1900 || year > 9999) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -768,8 +768,11 @@ export class GridDateColumn extends GridColumn {
|
|||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
static getValue(e, col) {
|
static getValue(e, col) {
|
||||||
|
if (e.target.tagName === 'INPUT') {
|
||||||
return getDateValue(e.target, col.dateValueFormatter);
|
return getDateValue(e.target, col.dateValueFormatter);
|
||||||
}
|
}
|
||||||
|
return e.target.innerText;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
|
@ -2652,7 +2652,7 @@ export class Grid {
|
|||||||
if (!readonly && GridColumnTypeEnum.isAlwaysEditing(col.type)) {
|
if (!readonly && GridColumnTypeEnum.isAlwaysEditing(col.type)) {
|
||||||
element = type.createEdit(e => this._onRowChanged(e, exists + i, col, e.target.checked, cell), col, exists + i);
|
element = type.createEdit(e => this._onRowChanged(e, exists + i, col, e.target.checked, cell), col, exists + i);
|
||||||
} else {
|
} else {
|
||||||
element = type.create(col, i, this);
|
element = type.create(col, exists + i, this);
|
||||||
if (typeof col.class === 'string') {
|
if (typeof col.class === 'string') {
|
||||||
type.setClass(element, col.class);
|
type.setClass(element, col.class);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user