fix: issue when input 'MM/dd/yyyy' date format
This commit is contained in:
parent
87e3d6c81b
commit
6458427c75
@ -32,16 +32,16 @@ export function createDateInput(min, max, element) {
|
|||||||
/**
|
/**
|
||||||
* 将日期转换为 `yyyy-MM-dd` 格式的字符串
|
* 将日期转换为 `yyyy-MM-dd` 格式的字符串
|
||||||
* @param {Date} dt 要转换的日期值
|
* @param {Date} dt 要转换的日期值
|
||||||
* @param {boolean} [utc] 是否采用 UTC 值
|
* @param {boolean} [local] 是否视日期为本地时间
|
||||||
* @returns 返回 `yyyy-MM-dd` 格式的字符串
|
* @returns 返回 `yyyy-MM-dd` 格式的字符串
|
||||||
*/
|
*/
|
||||||
export function toDateValue(dt, utc) {
|
export function toDateValue(dt, local) {
|
||||||
if (isNaN(dt)) {
|
if (isNaN(dt)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
const year = utc ? dt.getUTCFullYear() : dt.getFullYear();
|
const year = local ? dt.getFullYear() : dt.getUTCFullYear();
|
||||||
const month = String((utc ? dt.getUTCMonth() : dt.getMonth()) + 1).padStart(2, '0');
|
const month = String((local ? dt.getMonth() : dt.getUTCMonth()) + 1).padStart(2, '0');
|
||||||
const date = String(utc ? dt.getUTCDate() : dt.getDate()).padStart(2, '0');
|
const date = String(local ? dt.getDate() : dt.getUTCDate()).padStart(2, '0');
|
||||||
return `${year}-${month}-${date}`;
|
return `${year}-${month}-${date}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,16 +80,28 @@ export function setDateValue(element, val) {
|
|||||||
} else if (isNaN(val)) {
|
} else if (isNaN(val)) {
|
||||||
if (/^\d{4}-\d{2}-\d{2}/.test(val)) {
|
if (/^\d{4}-\d{2}-\d{2}/.test(val)) {
|
||||||
element.value = String(val).substring(0, 10);
|
element.value = String(val).substring(0, 10);
|
||||||
} else if (/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(val)) {
|
|
||||||
element.value = toDateValue(new Date(val));
|
|
||||||
} else {
|
} else {
|
||||||
element.value = '';
|
const e = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.exec(val);
|
||||||
|
if (e != null) {
|
||||||
|
const month = e[1].padStart(2, '0');
|
||||||
|
const day = e[2].padStart(2, '0');
|
||||||
|
const year = e[3];
|
||||||
|
element.value = `${year}-${month}-${day}`;
|
||||||
|
} else {
|
||||||
|
element.value = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!(val instanceof Date)) {
|
if (val instanceof Date) {
|
||||||
val = new Date((val - 621355968e9) / 1e4);
|
element.value = toDateValue(val);
|
||||||
|
} else {
|
||||||
|
const ticks = Number(val);
|
||||||
|
if (!isNaN(ticks) && ticks > 0) {
|
||||||
|
element.value = toDateValue(new Date((ticks - 621355968e9) / 1e4));
|
||||||
|
} else {
|
||||||
|
element.value = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
element.value = toDateValue(val, true);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
element.innerText = formatDate(val);
|
element.innerText = formatDate(val);
|
||||||
@ -117,7 +129,10 @@ export function getDateValue(element, formatter) {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
if (typeof formatter === 'function') {
|
if (typeof formatter === 'function') {
|
||||||
return formatter(date);
|
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
|
||||||
|
const day = String(date.getUTCDate()).padStart(2, '0');
|
||||||
|
// 使外部 formatter 不需要再处理 `getUTCDate` 亦或是 `getDate`
|
||||||
|
return formatter(new Date(`${year}-${month}-${day}T00:00:00`));
|
||||||
}
|
}
|
||||||
return String(date.getTime() * 1e4 + 621355968e9);
|
return String(date.getTime() * 1e4 + 621355968e9);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user