提取日期范围计算逻辑到工具函数

master
huangjinysf 3 months ago
parent 18fb0719dd
commit 003c3ebc3d

@ -0,0 +1,59 @@
/**
* 日期格式化工具函数
*/
/**
* 将日期对象格式化为 YYYY-MM-DD 格式的字符串
* @param {Date} date - 日期对象
* @returns {string} 格式化后的日期字符串
*/
export function formatDate(date) {
if (!date) return '';
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
/**
* 将日期对象格式化为 YYYY-MM-DD HH:mm:ss 格式的字符串
* @param {Date} date - 日期对象
* @returns {string} 格式化后的日期时间字符串
*/
export function formatDateTime(date) {
if (!date) return '';
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
/**
* 根据时间范围文字"近1年"计算开始和结束日期
* @param {string} timeRange - 时间范围文字
* @returns {Object} 包含开始和结束日期的对象
*/
export function getDateRangeByTimeRange(timeRange) {
const endDate = new Date();
const startDate = new Date();
if (timeRange === '近1年') {
startDate.setFullYear(endDate.getFullYear() - 1);
} else if (timeRange === '近2年') {
startDate.setFullYear(endDate.getFullYear() - 2);
} else if (timeRange === '近3年') {
startDate.setFullYear(endDate.getFullYear() - 3);
}
return {
start_date: formatDate(startDate),
end_date: formatDate(endDate)
};
}

@ -84,6 +84,7 @@
<script setup>
import { ref, reactive } from 'vue'
import { getDateRangeByTimeRange } from "@/utils/dateFormat"
import { AI_CONFIG, CURRENT_AI_MODEL } from "@/config/ai"
import { API_CONFIG } from "@/config/api"
@ -152,28 +153,8 @@ async function handleFetchData() {
}
//
const endDate = new Date();
const startDate = new Date();
//
if (props.selectedTimeRange === '近1年') {
startDate.setFullYear(endDate.getFullYear() - 1);
} else if (props.selectedTimeRange === '近2年') {
startDate.setFullYear(endDate.getFullYear() - 2);
} else if (props.selectedTimeRange === '近3年') {
startDate.setFullYear(endDate.getFullYear() - 3);
}
// YYYY-MM-DD
const formatDate = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
const startDateStr = formatDate(startDate);
const endDateStr = formatDate(endDate);
const dateRange = getDateRangeByTimeRange(props.selectedTimeRange);
const { start_date: startDateStr, end_date: endDateStr } = dateRange;
//
currentStartDate.value = startDateStr;

@ -119,6 +119,7 @@
<script setup name="WmsImportResult">
import { ref, reactive, shallowRef, watch } from 'vue'
import { API_CONFIG } from "@/config/api"
import { getDateRangeByTimeRange } from "@/utils/dateFormat"
import ParetoAnalysis from './ParetoAnalysis.vue'
import ItemAnalysis from './ItemAnalysis.vue'
import EmptyContent from './EmptyContent.vue'
@ -156,30 +157,7 @@ const currentComponent = shallowRef(EmptyContent)
watch(() => queryForm.value.selectedTimeRange, (newTimeRange) => {
// dateRange
if (currentComponent.value === RepairSummary) {
//
const endDate = new Date();
const startDate = new Date();
if (newTimeRange === '近1年') {
startDate.setFullYear(endDate.getFullYear() - 1);
} else if (newTimeRange === '近2年') {
startDate.setFullYear(endDate.getFullYear() - 2);
} else if (newTimeRange === '近3年') {
startDate.setFullYear(endDate.getFullYear() - 3);
}
// YYYY-MM-DD
const formatDate = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
dateRange.value = {
start_date: formatDate(startDate),
end_date: formatDate(endDate)
};
dateRange.value = getDateRangeByTimeRange(newTimeRange);
}
});
@ -203,27 +181,8 @@ const data = reactive({
function handlePareto() {
//
const endDate = new Date();
const startDate = new Date();
if (queryForm.value.selectedTimeRange === '近1年') {
startDate.setFullYear(endDate.getFullYear() - 1);
} else if (queryForm.value.selectedTimeRange === '近2年') {
startDate.setFullYear(endDate.getFullYear() - 2);
} else if (queryForm.value.selectedTimeRange === '近3年') {
startDate.setFullYear(endDate.getFullYear() - 3);
}
// YYYY-MM-DD
const formatDate = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
const startDateStr = formatDate(startDate);
const endDateStr = formatDate(endDate);
const dateRange = getDateRangeByTimeRange(queryForm.value.selectedTimeRange);
const { start_date: startDateStr, end_date: endDateStr } = dateRange;
// console.log(startDateStr, endDateStr);
// API
@ -266,33 +225,7 @@ function handleRepairSummary() {
console.log('切换到维修汇总组件,设备类型:', queryForm.value.selectedEquipmentType);
//
const endDate = new Date();
const startDate = new Date();
if (queryForm.value.selectedTimeRange === '近1年') {
startDate.setFullYear(endDate.getFullYear() - 1);
} else if (queryForm.value.selectedTimeRange === '近2年') {
startDate.setFullYear(endDate.getFullYear() - 2);
} else if (queryForm.value.selectedTimeRange === '近3年') {
startDate.setFullYear(endDate.getFullYear() - 3);
}
// YYYY-MM-DD
const formatDate = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
const startDateStr = formatDate(startDate);
const endDateStr = formatDate(endDate);
// dateRange
dateRange.value = {
start_date: startDateStr,
end_date: endDateStr
};
dateRange.value = getDateRangeByTimeRange(queryForm.value.selectedTimeRange);
//
currentComponent.value = RepairSummary;

@ -84,6 +84,7 @@
<script setup>
import { ref, reactive } from 'vue'
import { getDateRangeByTimeRange } from "@/utils/dateFormat"
import { AI_CONFIG, CURRENT_AI_MODEL } from "@/config/ai"
import { API_CONFIG } from "@/config/api"
@ -152,28 +153,8 @@ async function handleFetchData() {
}
//
const endDate = new Date();
const startDate = new Date();
//
if (props.selectedTimeRange === '近1年') {
startDate.setFullYear(endDate.getFullYear() - 1);
} else if (props.selectedTimeRange === '近2年') {
startDate.setFullYear(endDate.getFullYear() - 2);
} else if (props.selectedTimeRange === '近3年') {
startDate.setFullYear(endDate.getFullYear() - 3);
}
// YYYY-MM-DD
const formatDate = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
const startDateStr = formatDate(startDate);
const endDateStr = formatDate(endDate);
const dateRange = getDateRangeByTimeRange(props.selectedTimeRange);
const { start_date: startDateStr, end_date: endDateStr } = dateRange;
//
currentStartDate.value = startDateStr;

@ -49,6 +49,7 @@
<script setup name="WmsImportResult">
import { ref, reactive, shallowRef } from 'vue'
import { API_CONFIG } from "@/config/api"
import { getDateRangeByTimeRange } from "@/utils/dateFormat"
import ParetoAnalysis from './ParetoAnalysis.vue'
import ItemAnalysis from './ItemAnalysis.vue'
import EquipmentStatus from './EquipmentStatus.vue'
@ -96,27 +97,8 @@ const data = reactive({
function handlePareto() {
//
const endDate = new Date();
const startDate = new Date();
if (queryForm.value.selectedTimeRange === '近1年') {
startDate.setFullYear(endDate.getFullYear() - 1);
} else if (queryForm.value.selectedTimeRange === '近2年') {
startDate.setFullYear(endDate.getFullYear() - 2);
} else if (queryForm.value.selectedTimeRange === '近3年') {
startDate.setFullYear(endDate.getFullYear() - 3);
}
// YYYY-MM-DD
const formatDate = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
const startDateStr = formatDate(startDate);
const endDateStr = formatDate(endDate);
const dateRange = getDateRangeByTimeRange(queryForm.value.selectedTimeRange);
const { start_date: startDateStr, end_date: endDateStr } = dateRange;
// console.log(startDateStr, endDateStr);
// API

@ -84,6 +84,7 @@
<script setup>
import { ref, reactive } from 'vue'
import { getDateRangeByTimeRange } from "@/utils/dateFormat"
import { AI_CONFIG, CURRENT_AI_MODEL } from "@/config/ai"
import { API_CONFIG } from "@/config/api"
@ -152,28 +153,8 @@ async function handleFetchData() {
}
//
const endDate = new Date();
const startDate = new Date();
//
if (props.selectedTimeRange === '近1年') {
startDate.setFullYear(endDate.getFullYear() - 1);
} else if (props.selectedTimeRange === '近2年') {
startDate.setFullYear(endDate.getFullYear() - 2);
} else if (props.selectedTimeRange === '近3年') {
startDate.setFullYear(endDate.getFullYear() - 3);
}
// YYYY-MM-DD
const formatDate = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
const startDateStr = formatDate(startDate);
const endDateStr = formatDate(endDate);
const dateRange = getDateRangeByTimeRange(props.selectedTimeRange);
const { start_date: startDateStr, end_date: endDateStr } = dateRange;
//
currentStartDate.value = startDateStr;

@ -92,6 +92,7 @@
<script setup name="WmsImportResult">
import { ref, reactive, shallowRef } from 'vue'
import { API_CONFIG } from "@/config/api"
import { getDateRangeByTimeRange } from "@/utils/dateFormat"
import ParetoAnalysis from './ParetoAnalysis.vue'
import ItemAnalysis from './ItemAnalysis.vue'
import EmptyContent from './EmptyContent.vue'
@ -137,27 +138,8 @@ const data = reactive({
function handlePareto() {
//
const endDate = new Date();
const startDate = new Date();
if (queryForm.value.selectedTimeRange === '近1年') {
startDate.setFullYear(endDate.getFullYear() - 1);
} else if (queryForm.value.selectedTimeRange === '近2年') {
startDate.setFullYear(endDate.getFullYear() - 2);
} else if (queryForm.value.selectedTimeRange === '近3年') {
startDate.setFullYear(endDate.getFullYear() - 3);
}
// YYYY-MM-DD
const formatDate = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
const startDateStr = formatDate(startDate);
const endDateStr = formatDate(endDate);
const dateRange = getDateRangeByTimeRange(queryForm.value.selectedTimeRange);
const { start_date: startDateStr, end_date: endDateStr } = dateRange;
// console.log(startDateStr, endDateStr);
// API

Loading…
Cancel
Save