时间格式化

This commit is contained in:
cwchen 2025-11-24 10:31:43 +08:00
parent 2e3c457952
commit 7cbd2bfe98
1 changed files with 64 additions and 63 deletions

View File

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