| | |
| | | // - YYYYMMDD |
| | | // - yyyy-MM-dd HH:mm:ss |
| | | console.log('尝试格式化日期字符串:', dateStr) |
| | | |
| | | // 如果输入为空或无效,返回空字符串 |
| | | if (!dateStr || typeof dateStr !== 'string') { |
| | | console.warn('日期字符串无效:', dateStr) |
| | | return '' |
| | | } |
| | | |
| | | // 清洗日期字符串 |
| | | let cleaned = dateStr |
| | | .replace(/[年月]/g, '-') |
| | | .replace(/[日号]/g, ' ') // 日/号 → 空格,保留日期和时间的分隔 |
| | |
| | | .replace(/秒/g, '') |
| | | .replace(/\s+/g, ' ') // 多个空格合并为一个 |
| | | .trim() |
| | | |
| | | console.log('清理后的日期字符串:', cleaned) |
| | | |
| | | // 分离日期和时间部分 |
| | | const parts = cleaned.split(' ') |
| | | let datePart = parts[0] || '' |
| | | let timePart = parts[1] || '' |
| | | |
| | | let dateResult = '' |
| | | |
| | | // 处理日期部分 |
| | | // 如果是YYMMDD格式 |
| | | if (/^\d{6}$/.test(cleaned)) { |
| | | const year = '20' + cleaned.substring(0, 2) |
| | | const month = cleaned.substring(2, 4) |
| | | const day = cleaned.substring(4, 6) |
| | | dateResult = `${year}-${month}-${day}`; |
| | | } |
| | | // 如果是YYYYMMDD格式 |
| | | else if (/^\d{8}$/.test(cleaned)) { |
| | | const year = cleaned.substring(0, 4) |
| | | const month = cleaned.substring(4, 6) |
| | | const day = cleaned.substring(6, 8) |
| | | if (/^\d{6}$/.test(datePart)) { |
| | | const year = '20' + datePart.substring(0, 2) |
| | | const month = datePart.substring(2, 4) |
| | | const day = datePart.substring(4, 6) |
| | | dateResult = `${year}-${month}-${day}` |
| | | } |
| | | // 如果已经是合理格式,直接使用 |
| | | else if (cleaned.match(/^\d{4}[-/]\d{1,2}[-/]\d{1,2}$/)) { |
| | | dateResult = cleaned.replace(/[//]/g, '-') |
| | | // 如果是YYYYMMDD格式 |
| | | else if (/^\d{8}$/.test(datePart)) { |
| | | const year = datePart.substring(0, 4) |
| | | const month = datePart.substring(4, 6) |
| | | const day = datePart.substring(6, 8) |
| | | dateResult = `${year}-${month}-${day}` |
| | | } |
| | | // 如果已经包含时分秒,直接返回 |
| | | else if (cleaned.match(/^\d{4}[-/]\d{1,2}[-/]\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}$/)) { |
| | | return cleaned.replace(/[//]/g, '-') |
| | | } |
| | | // 如果包含时分但缺少秒(yyyy-MM-dd HH:mm:) |
| | | else if (cleaned.match(/^\d{4}[-/]\d{1,2}[-/]\d{1,2}\s+\d{1,2}:\d{1,2}:$/)) { |
| | | // 去掉末尾的冒号,补上秒数00 |
| | | return cleaned.replace(/[//]/g, '-').replace(/:$/, '') + ':00' |
| | | } |
| | | // 如果只包含时分(yyyy-MM-dd HH:mm) |
| | | else if (cleaned.match(/^\d{4}[-/]\d{1,2}[-/]\d{1,2}\s+\d{1,2}:\d{1,2}$/)) { |
| | | return cleaned.replace(/[//]/g, '-') + ':00' |
| | | // 如果是yyyy-MM-dd或yyyy/MM/dd格式 |
| | | else if (datePart.match(/^\d{4}[-\/]\d{1,2}[-\/]\d{1,2}$/)) { |
| | | dateResult = datePart.replace(/\//g, '-') |
| | | } |
| | | else { |
| | | dateResult = dateStr |
| | | dateResult = datePart |
| | | } |
| | | |
| | | // 如果日期格式正确,添加默认时分秒 00:00:00 |
| | | if (dateResult && dateResult.match(/^\d{4}-\d{1,2}-\d{1,2}$/)) { |
| | | return dateResult + ' 00:00:00' |
| | | // 验证日期部分是否有效 |
| | | if (!dateResult.match(/^\d{4}-\d{1,2}-\d{1,2}$/)) { |
| | | console.warn('日期格式不正确:', dateResult) |
| | | return '' |
| | | } |
| | | |
| | | return dateResult |
| | | // 处理时间部分 |
| | | let timeResult = '00:00:00' // 默认时间 |
| | | |
| | | if (timePart) { |
| | | // 移除末尾多余的冒号 |
| | | timePart = timePart.replace(/:+$/, '') |
| | | |
| | | // 分割时、分、秒 |
| | | const timeParts = timePart.split(':') |
| | | const hour = timeParts[0] || '00' |
| | | const minute = timeParts[1] || '00' |
| | | const second = timeParts[2] || '00' |
| | | |
| | | // 验证时间数字是否有效 |
| | | const hourNum = parseInt(hour, 10) |
| | | const minuteNum = parseInt(minute, 10) |
| | | const secondNum = parseInt(second, 10) |
| | | |
| | | if (!isNaN(hourNum) && !isNaN(minuteNum) && !isNaN(secondNum) && |
| | | hourNum >= 0 && hourNum < 24 && minuteNum >= 0 && minuteNum < 60 && secondNum >= 0 && secondNum < 60) { |
| | | // 补齐两位数 |
| | | timeResult = `${String(hourNum).padStart(2, '0')}:${String(minuteNum).padStart(2, '0')}:${String(secondNum).padStart(2, '0')}` |
| | | } else { |
| | | console.warn('时间数值超出范围,使用默认值00:00:00') |
| | | } |
| | | } |
| | | |
| | | const finalResult = `${dateResult} ${timeResult}` |
| | | console.log('最终格式化结果:', finalResult) |
| | | |
| | | return finalResult |
| | | } |
| | | } |
| | | } |