/**
*
*
* @return string ---返回字符串
*/
export function getTimeGreeting() {
const date=new Date()
const hours = date.getHours();
let greeting = '';
if (hours >= 5 && hours < 12) {
greeting = '早上好';
} else if (hours >= 12 && hours < 18) {
greeting = '下午好';
} else {
greeting = '晚上好';
}
return greeting;
}
/**
*
* @param time ---时间戳
* @return string ---返回xxxx-xx-xx类型的日期
*/
export function timeFormatting(time: string | number | Date){//获取日期
const date =new Date(time)
const year=date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const formateDate=`${year}-${month}-${day}`
return formateDate
}
/**
*
* @return ---一段时间[xx-xx,xx-xx....]
*/
export function getMonthDays(startDateStr: string | number | Date, endDateStr: string | number | Date) {
const startDate = startDateStr ? new Date(startDateStr) : new Date();
const endDate = endDateStr ? new Date(endDateStr) : new Date();
const xAxisData = [];
const currentDate = new Date(startDate.getTime());
while (currentDate <= endDate) {
const formattedDate = currentDate.toLocaleDateString(undefined, { month: '2-digit', day: '2-digit' });
xAxisData.push(formattedDate);
currentDate.setDate(currentDate.getDate() + 1);
}
return xAxisData;
}
/**
*
* @return ---一段时间[xxxx-xx-xxx.....]
*/
export function getCurrentMonthDates(startDate: string | number | Date, endDate: string | number | Date) {
const start = new Date(startDate);
const end = new Date(endDate);
const dates = [];
while (start <= end) {
const year = start.getFullYear();
const month = start.getMonth() + 1;
const day = start.getDate();
const dateStr = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
dates.push(dateStr);
start.setDate(start.getDate() + 1);
}
return dates;
}
/**
* 是一个element组件的时间选择快捷方式
* @param shortcutType --字符串
* @return object --返回一个对象 {text:xxx,value:[xxx,xxx]}
*/
export function dateTimePeriod(shortcutType: string) {
const now = new Date();
const currentYear = now.getFullYear();
const currentMonth = now.getMonth();
const getDateWithOffset = (offset: number) => {
const date = new Date(now.getTime() + offset * 24 * 60 * 60 * 1000);
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
};
const ranges = {
Today: { text: "Today", value: [now, now] },
Yesterday: {
text: "Yesterday",
value: [
new Date(currentYear, currentMonth, now.getDate() - 1),
new Date(currentYear, currentMonth, now.getDate() - 1),
],
},
"Last 7 Days": {
text: "Last 7 Days",
value: [getDateWithOffset(-7), getDateWithOffset(-1)],
},
"Last 30 Days": {
text: "Last 30 Days",
value: [getDateWithOffset(-30), getDateWithOffset(-1)],
},
"This Month": {
text: "This Month",
value: [new Date(currentYear, currentMonth, 1), now],
},
"Last Month": {
text: "Last Month",
value: [
new Date(currentYear, currentMonth - 1, 1),
new Date(currentYear, currentMonth, 0),
],
},
"This Quarter": {
text: "This Quarter",
value: [
new Date(currentYear, Math.floor(currentMonth / 3) * 3, 1),
now,
],
},
"Last Quarter": {
text: "Last Quarter",
value: [
new Date(currentYear, Math.floor(currentMonth / 3) * 3 - 3, 1),
new Date(currentYear, Math.floor(currentMonth / 3) * 3, 0),
],
},
};
return ranges[shortcutType as keyof typeof ranges] || [now, now];
}
时间方法--根据不同时间段展示
于 2023-09-12 14:18:22 首次发布