时间格式化
This commit is contained in:
parent
2e3c457952
commit
7cbd2bfe98
|
|
@ -86,7 +86,7 @@ export function selectDictLabel(datas, value) {
|
||||||
|
|
||||||
// 回显数据字典(字符串、数组)
|
// 回显数据字典(字符串、数组)
|
||||||
export function selectDictLabels(datas, value, separator) {
|
export function selectDictLabels(datas, value, separator) {
|
||||||
if (value === undefined || value.length ===0) {
|
if (value === undefined || value.length === 0) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
|
|
@ -230,14 +230,15 @@ export function blobValidate(data) {
|
||||||
// 将年月日格式转换为yyyy-MM-dd格式
|
// 将年月日格式转换为yyyy-MM-dd格式
|
||||||
export function formatDate(dateStr) {
|
export function formatDate(dateStr) {
|
||||||
if (!dateStr) return '';
|
if (!dateStr) return '';
|
||||||
// 提取年、月、日
|
const cleanedStr = dateStr.replace(/\s/g, '');
|
||||||
const match = dateStr.match(/(\d{4})年(\d{1,2})月(\d{1,2})日/);
|
// 提取年、月、日
|
||||||
if (!match) return dateStr;
|
const match = cleanedStr.match(/(\d{4})年(\d{1,2})月(\d{1,2})日/);
|
||||||
const year = match[1];
|
if (!match) return cleanedStr;
|
||||||
const month = match[2].padStart(2, '0'); // 月份补零
|
const year = match[1];
|
||||||
const day = match[3].padStart(2, '0'); // 日期补零
|
const month = match[2].padStart(2, '0'); // 月份补零
|
||||||
|
const day = match[3].padStart(2, '0'); // 日期补零
|
||||||
|
|
||||||
return `${year}-${month}-${day}`;
|
return `${year}-${month}-${day}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将中文日期(包括毕业证书上的日期)转换为yyyy-MM-dd格式
|
// 将中文日期(包括毕业证书上的日期)转换为yyyy-MM-dd格式
|
||||||
|
|
@ -245,64 +246,64 @@ export function convertChineseDate(chineseDate) {
|
||||||
if (!chineseDate) return '';
|
if (!chineseDate) return '';
|
||||||
|
|
||||||
const chineseNumbers = {
|
const chineseNumbers = {
|
||||||
'〇': '0', '○': '0', '零': '0', '○': '0',
|
'〇': '0', '○': '0', '零': '0', '○': '0',
|
||||||
'一': '1', '二': '2', '三': '3', '四': '4', '五': '5',
|
'一': '1', '二': '2', '三': '3', '四': '4', '五': '5',
|
||||||
'六': '6', '七': '7', '八': '8', '九': '9', '十': '10'
|
'六': '6', '七': '7', '八': '8', '九': '9', '十': '10'
|
||||||
};
|
};
|
||||||
|
|
||||||
const monthMap = {
|
const monthMap = {
|
||||||
'一': '01', '二': '02', '三': '03', '四': '04', '五': '05', '六': '06',
|
'一': '01', '二': '02', '三': '03', '四': '04', '五': '05', '六': '06',
|
||||||
'七': '07', '八': '08', '九': '09', '十': '10', '十一': '11', '十二': '12',
|
'七': '07', '八': '08', '九': '09', '十': '10', '十一': '11', '十二': '12',
|
||||||
'1': '01', '2': '02', '3': '03', '4': '04', '5': '05', '6': '06',
|
'1': '01', '2': '02', '3': '03', '4': '04', '5': '05', '6': '06',
|
||||||
'7': '07', '8': '08', '9': '09', '10': '10', '11': '11', '12': '12'
|
'7': '07', '8': '08', '9': '09', '10': '10', '11': '11', '12': '12'
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let dateStr = chineseDate.toString();
|
let dateStr = chineseDate.toString();
|
||||||
|
|
||||||
// 处理年份中的中文数字
|
// 处理年份中的中文数字
|
||||||
let yearPart = dateStr.split('年')[0];
|
let yearPart = dateStr.split('年')[0];
|
||||||
let convertedYear = '';
|
let convertedYear = '';
|
||||||
for (let char of yearPart) {
|
for (let char of yearPart) {
|
||||||
convertedYear += chineseNumbers[char] || char;
|
convertedYear += chineseNumbers[char] || char;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取月日部分
|
||||||
|
const monthDayPart = dateStr.split('年')[1];
|
||||||
|
if (!monthDayPart) return chineseDate;
|
||||||
|
|
||||||
|
const monthMatch = monthDayPart.match(/(.+?)月/);
|
||||||
|
const dayMatch = monthDayPart.match(/(\d+|[一二三四五六七八九十]+)日/);
|
||||||
|
|
||||||
|
const year = convertedYear;
|
||||||
|
let month = monthMatch ? (monthMap[monthMatch[1]] || monthMatch[1].padStart(2, '0')) : '01';
|
||||||
|
let day = '01';
|
||||||
|
|
||||||
|
// 处理日
|
||||||
|
if (dayMatch) {
|
||||||
|
let dayStr = dayMatch[1];
|
||||||
|
// 转换中文日
|
||||||
|
if (isNaN(dayStr)) {
|
||||||
|
if (dayStr === '十') day = '10';
|
||||||
|
else if (dayStr.startsWith('十')) day = '1' + (chineseNumbers[dayStr[1]] || '0');
|
||||||
|
else if (dayStr.endsWith('十')) day = (chineseNumbers[dayStr[0]] || '0') + '0';
|
||||||
|
else day = dayStr.split('').map(char => chineseNumbers[char] || char).join('');
|
||||||
|
} else {
|
||||||
|
day = dayStr;
|
||||||
}
|
}
|
||||||
|
day = day.padStart(2, '0');
|
||||||
|
}
|
||||||
|
|
||||||
// 提取月日部分
|
// 验证日期有效性
|
||||||
const monthDayPart = dateStr.split('年')[1];
|
const finalDate = `${year}-${month}-${day}`;
|
||||||
if (!monthDayPart) return chineseDate;
|
if (!isNaN(new Date(finalDate).getTime())) {
|
||||||
|
return finalDate;
|
||||||
|
}
|
||||||
|
|
||||||
const monthMatch = monthDayPart.match(/(.+?)月/);
|
return chineseDate;
|
||||||
const dayMatch = monthDayPart.match(/(\d+|[一二三四五六七八九十]+)日/);
|
|
||||||
|
|
||||||
const year = convertedYear;
|
|
||||||
let month = monthMatch ? (monthMap[monthMatch[1]] || monthMatch[1].padStart(2, '0')) : '01';
|
|
||||||
let day = '01';
|
|
||||||
|
|
||||||
// 处理日
|
|
||||||
if (dayMatch) {
|
|
||||||
let dayStr = dayMatch[1];
|
|
||||||
// 转换中文日
|
|
||||||
if (isNaN(dayStr)) {
|
|
||||||
if (dayStr === '十') day = '10';
|
|
||||||
else if (dayStr.startsWith('十')) day = '1' + (chineseNumbers[dayStr[1]] || '0');
|
|
||||||
else if (dayStr.endsWith('十')) day = (chineseNumbers[dayStr[0]] || '0') + '0';
|
|
||||||
else day = dayStr.split('').map(char => chineseNumbers[char] || char).join('');
|
|
||||||
} else {
|
|
||||||
day = dayStr;
|
|
||||||
}
|
|
||||||
day = day.padStart(2, '0');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 验证日期有效性
|
|
||||||
const finalDate = `${year}-${month}-${day}`;
|
|
||||||
if (!isNaN(new Date(finalDate).getTime())) {
|
|
||||||
return finalDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
return chineseDate;
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('日期转换错误:', error);
|
console.error('日期转换错误:', error);
|
||||||
return chineseDate;
|
return chineseDate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue