Compare commits
No commits in common. "c78e445a24e4b7e2fc842c4306fd06792467191f" and "87e3d6c81b56df4b22f3bec090e91162e8cb86a2" have entirely different histories.
c78e445a24
...
87e3d6c81b
@ -32,16 +32,16 @@ export function createDateInput(min, max, element) {
|
|||||||
/**
|
/**
|
||||||
* 将日期转换为 `yyyy-MM-dd` 格式的字符串
|
* 将日期转换为 `yyyy-MM-dd` 格式的字符串
|
||||||
* @param {Date} dt 要转换的日期值
|
* @param {Date} dt 要转换的日期值
|
||||||
* @param {boolean} [local] 是否视日期为本地时间
|
* @param {boolean} [utc] 是否采用 UTC 值
|
||||||
* @returns 返回 `yyyy-MM-dd` 格式的字符串
|
* @returns 返回 `yyyy-MM-dd` 格式的字符串
|
||||||
*/
|
*/
|
||||||
export function toDateValue(dt, local) {
|
export function toDateValue(dt, utc) {
|
||||||
if (isNaN(dt)) {
|
if (isNaN(dt)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
const year = local ? dt.getFullYear() : dt.getUTCFullYear();
|
const year = utc ? dt.getUTCFullYear() : dt.getFullYear();
|
||||||
const month = String((local ? dt.getMonth() : dt.getUTCMonth()) + 1).padStart(2, '0');
|
const month = String((utc ? dt.getUTCMonth() : dt.getMonth()) + 1).padStart(2, '0');
|
||||||
const date = String(local ? dt.getDate() : dt.getUTCDate()).padStart(2, '0');
|
const date = String(utc ? dt.getUTCDate() : dt.getDate()).padStart(2, '0');
|
||||||
return `${year}-${month}-${date}`;
|
return `${year}-${month}-${date}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,28 +80,16 @@ 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 {
|
||||||
const e = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.exec(val);
|
element.value = '';
|
||||||
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)) {
|
||||||
element.value = toDateValue(val);
|
val = new Date((val - 621355968e9) / 1e4);
|
||||||
} 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);
|
||||||
@ -129,10 +117,7 @@ export function getDateValue(element, formatter) {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
if (typeof formatter === 'function') {
|
if (typeof formatter === 'function') {
|
||||||
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
|
return formatter(date);
|
||||||
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);
|
||||||
}
|
}
|
||||||
@ -145,33 +130,7 @@ export function getDateValue(element, formatter) {
|
|||||||
*/
|
*/
|
||||||
export class DateSelector {
|
export class DateSelector {
|
||||||
_var = {
|
_var = {
|
||||||
/**
|
options: null
|
||||||
* @type {HTMLInputElement}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
el: null,
|
|
||||||
options: {
|
|
||||||
/**
|
|
||||||
* @type {boolean?}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
enabled: true,
|
|
||||||
/**
|
|
||||||
* @type {string?}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
minDate: null,
|
|
||||||
/**
|
|
||||||
* @type {string?}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
maxDate: null,
|
|
||||||
/**
|
|
||||||
* @type {DateFormatterCallback?}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
valueFormatter: null
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -282,11 +241,6 @@ export class DateSelector {
|
|||||||
this._var.options.maxDate = date;
|
this._var.options.maxDate = date;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
* @param {Date} date
|
|
||||||
* @returns {Date | any}
|
|
||||||
*/
|
|
||||||
_getDate(date) {
|
_getDate(date) {
|
||||||
if (date instanceof Date && !isNaN(date)) {
|
if (date instanceof Date && !isNaN(date)) {
|
||||||
const year = date.getUTCFullYear();
|
const year = date.getUTCFullYear();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user