diff --git a/src/utils/common.js b/src/utils/common.js index 815666c..71a0267 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -34,7 +34,7 @@ export function getServicePriceDesc(price, price_unit, unit_num, unit_name) { } /** - * 获取格式化的日期字符串 + * 获取格式化的日期字符串--是当下日期 * @param {string} format - 日期格式,可选值:'YYYY-MM-DD', 'YYYY-MM', 'YYYY', 'HH:mm:ss' 等 * @returns {string} 格式化后的日期字符串 * @@ -65,6 +65,54 @@ export const getFormattedDate = (format = 'YYYY-MM-DD') => { return formats[format] || formats['YYYY-MM-DD']; }; +// 根据指定日期获取年月格式字符串 +/** + * 日期格式化函数 + * + * @param {string} dateStr - 日期字符串,可被Date构造函数解析 + * @param {string} [format='yyyy-mm'] - 输出格式,可选值:'year'、'month'、'year-month'、'yyyy'、'mm'、'm'、'yyyy-mm'、'yyyy-m' + * @returns {string} 格式化后的日期字符串,如果日期无效则返回'Invalid date' + * 使用示例 + * 1. console.log(getDate('2023-12-31')); // 输出: "2023-12" + * 2. console.log(getDate('2023-12-31', 'yyyy' || 'year')); // 输出: "2023" + * 3. console.log(getDate('2023-12-31', 'yyyy-m')); // 输出: "2023-12" + * 4. console.log(getDate('2023-12-31', 'm' || 'month')); // 输出: "12" + * 7. console.log(getDate('2023-12-31', 'year-month')); // 输出: "2023-12" + * 8. console.log(getDate('2023-12-31', 'yyyy/mm')); // 输出: "Invalid date"(因为格式'yyyy/mm'不在可选值中) + * 9.console.log(getDate('2025-03-15T10:30:00', 'yyyy-mm')); // ISO格式 + * 10.console.log(getDate('March 15, 2025', 'yyyy'));//2023 // 英文日期 + * 11.console.log(getDate('2025/03/15', 'year-month')); // 输出: "2025-03" + */ +export function getDate(dateStr, format = 'yyyy-mm') { + try { + const date = new Date(dateStr); + + // 检查日期是否有效 + if (isNaN(date.getTime())) { + throw new Error('日期无效'); + } + + const year = date.getFullYear(); + const month = date.getMonth() + 1; + + const formats = { + 'year': `${year}`, + 'month': `${month.toString().padStart(2, '0')}`, + 'year-month': `${year}-${month.toString().padStart(2, '0')}`, + 'yyyy': `${year}`, + 'mm': `${month.toString().padStart(2, '0')}`, + 'm': `${month}`, + 'yyyy-mm': `${year}-${month.toString().padStart(2, '0')}`, //补0 + 'yyyy-m': `${year}-${month}`//不补0 + }; + + return formats[format] || formats['yyyy-mm']; + } catch (error) { + console.error('Error parsing date:', error); + return 'Invalid date'; + } +} + //金额千分符 会在整数后添加两个0 --适用于直接显示的 export function stateFormat(row, column, cellValue) { if (cellValue) { diff --git a/src/views/super/Ranking/RankBatchList.vue b/src/views/super/Ranking/RankBatchList.vue index 3f22a53..5a98435 100644 --- a/src/views/super/Ranking/RankBatchList.vue +++ b/src/views/super/Ranking/RankBatchList.vue @@ -183,6 +183,8 @@ import GuipTable from '@/components/GuipTable.vue'; import CustomDropdown from '@/components/CustomDropdown.vue'; import SvgIcon from '@/components/SvgIcon.vue'; import GuipToolTip from '@/components/GuipToolTip.vue'; +import {getDate,getFormattedDate} from "@/utils/common.js" + // import HoverImage from '@/components/super/HoverImage.vue'; export default { name: 'rank_batch_list', @@ -278,26 +280,13 @@ export default { }, getNowDate() { const viewdesc = this.viewDesc[this.view]; - const now = new Date(); - const year = now.getFullYear(); - const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始,补零 - const currentYearMonth = `${year}-${month}`; + const currentYearMonth = getFormattedDate('YYYY-MM') this.textDesc = viewdesc + currentYearMonth; return `${currentYearMonth}` }, - getDate(dateStr) { - const date = new Date(dateStr); - const year = date.getFullYear(); // 2025 - const month = date.getMonth() + 1; // 3 (表示3月) - if (this.view == 'year') { - return `${year}` - } else { - return `${year}-${month}` - } - }, handleDateChange(date) { const viewdesc = this.viewDesc[this.view]; - this.text = this.getDate(date) + this.text = this.view == 'year' ? getDate(date,'yyyy') : getDate(date,'yyyy-mm') this.textDesc = viewdesc + this.text; this.selectedDate = date; localStorage.setItem('date', JSON.stringify(date)) diff --git a/src/views/super/Ranking/RankDetail.vue b/src/views/super/Ranking/RankDetail.vue index 1b779b6..21b8794 100644 --- a/src/views/super/Ranking/RankDetail.vue +++ b/src/views/super/Ranking/RankDetail.vue @@ -233,6 +233,7 @@ import DateSelect from '@/components/super/DateSelect.vue'; import CustomDropdown from '@/components/CustomDropdown.vue'; import HoverImage from "@/components/super/HoverImage.vue"; import Pagination from "@/components/Pagination.vue"; +import {getFormattedDate,getDate} from "@/utils/common.js" export default { name: 'rank_detail', @@ -313,7 +314,7 @@ export default { document.title = this.pageTitle; this.text = "" - if (this.showDateSelect) this.text = this.getNowYear() + if (this.showDateSelect) this.text = getFormattedDate('YYYY') this.dataRank = this.rank_type this.dataType = this.type this.aid = this.$route.query.aid @@ -329,24 +330,8 @@ export default { handleUpdateView(newView) { this.view = newView; }, - getNowYear() { - const now = new Date(); - const year = now.getFullYear(); - const currentYearMonth = `${year}`; - return `${currentYearMonth}` - }, - getDate(dateStr) { - const date = new Date(dateStr); - const year = date.getFullYear(); // 2025 - const month = date.getMonth() + 1; // 3 (表示3月) - if (this.view == 'year') { - return `${year}` - } else { - return `${year}-${month}` - } - }, handleDateChange(date) { - this.text = this.getDate(date) + this.text = this.view == 'year' ? getDate(date,'yyyy') : getDate(date,'yyyy-mm') this.selectedDate = date; localStorage.setItem('date', JSON.stringify(date)) diff --git a/src/views/super/Ranking/RankList.vue b/src/views/super/Ranking/RankList.vue index 275a878..0270543 100644 --- a/src/views/super/Ranking/RankList.vue +++ b/src/views/super/Ranking/RankList.vue @@ -218,7 +218,7 @@ import DateSelect from '@/components/super/DateSelect.vue'; import CustomDropdown from '@/components/CustomDropdown.vue'; import HoverImage from "@/components/super/HoverImage.vue"; import GuipTable from "@/components/GuipTable.vue"; - +import {getFormattedDate,getDate} from "@/utils/common.js" export default { name: 'rank_list', props: { @@ -306,7 +306,7 @@ export default { init() { document.title = this.pageTitle; - this.text = this.getNowDate() + this.text = getFormattedDate('YYYY-MM') this.dataType = this.type this.dataRank = this.rank_type this.aid = this.$route.query.aid @@ -327,23 +327,6 @@ export default { handleUpdateView(newView) { this.view = newView; }, - getNowDate() { - const now = new Date(); - const year = now.getFullYear(); - const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始,补零 - const currentYearMonth = `${year}-${month}`; - return `${currentYearMonth}` - }, - getDate(dateStr) { - const date = new Date(dateStr); - const year = date.getFullYear(); // 2025 - const month = date.getMonth() + 1; // 3 (表示3月) - if (this.view == 'year') { - return `${year}` - } else { - return `${year}-${month}` - } - }, setDateView(){ if (/^\d{4}$/.test(this.text)) { this.view = 'year' @@ -352,7 +335,7 @@ export default { } }, handleDateChange(date) { - this.text = this.getDate(date) + this.text = this.view == 'year' ? getDate(date,'yyyy') : getDate(date,'yyyy-mm') this.selectedDate = date; localStorage.setItem('date', JSON.stringify(date))