数据回显
/**
函数名称:数据回显
函数作者:诺小D && 不实名同事
参数说明:{
val:String 需要被回显的key或者id 等关键字
arr:Array 列表或者树结构
returnData:最后返回的结果
}
注意事项:childs、children、ognztListVos后期配合后端子结构数据添加判断条件
**/
export function funEcho (val, arr = [], value = 'value', label = 'label') {
let returnData = '-';
// console.log(val, arr);
function digui(val, arr, value, label) {
for (let i of arr) {
if (i[value] == val) {
returnData = i[label];
}
//childs ||children ||children 根据后端返回树的子级情况进行更改
if ((i.childs && i.childs.length) || (i.children && i.children.length) || (i.ognztListVos && i.ognztListVos.length)) {
digui(val, i.childs || i.children || i.ognztListVos, value, label)
}
}
}
digui(val, arr, value , label );
return returnData
}
起始时间算差
export function timeDifference(startTime, endTime,) {
// 传入起始时间 没有结束时间会自动获取 也做了开始时间的容错验证
if(!startTime){
return
}
var stime = Date.parse(new Date(startTime));
var etime;
if(!endTime){
etime = new Date().valueOf();
}else{
etime = Date.parse(new Date(endTime));
}
// 此处是毫秒 下面除的是秒所有需要除以1000
var runTime = (parseInt(etime - stime)/1000)
var day = Math.floor(runTime / 86400)
runTime = runTime % 86400
var hour = Math.floor(runTime / 3600)
runTime = runTime % 3600
var minute = Math.floor(runTime / 60)
runTime = Math.floor(runTime % 60)
var second = runTime;
var time = (day?day + "天":'')+ (hour?hour+'时':'')+ (minute?minute + "分":'') + (second?second+'秒':'');
return time;
}
文件类型
//文件格式不多,根据自己需求加就行了
export function formatFileType(type) {
type = type.toUpperCase()
if (['TXT'].includes(type)) {
return 'text'
}
else if (['PDF'].includes(type)) {
return 'pdf'
}
else if (['HTML', 'HTM'].includes(type)) {
return 'link'
}
else if (['DOC', 'DOCX'].includes(type)) {
return 'word'
}
else if (['XLS', 'XLSX'].includes(type)) {
return 'excel'
}
else if (['PPT', 'PPTX'].includes(type)) {
return 'ppt'
}
else if (['JPG', 'JPEG', 'PNG'].includes(type)) {
return 'pic'
}else if (['MP4', 'MOV', 'AVI', 'WMV'].includes(type)) {
return 'video'
} else if (['mp3', 'wav'].includes(type)) {
return 'mp3'
}
else {
return 'unknown'
}
}
格式化日期
/**
函数名称:格式化时间
函数作者:不实名同事
参数说明:{
timestamp: String 需要格式化的时间, 若未指定, 则默认格式化当前系统时间
formatType: String 支持以下格式:纯数字 / YYYY-MM-DD HH:MM:SS / YYYY-MM-DD / MM/DD / HH:MM:SS / MM-DD HH:MM / 年月日时分秒, 若未指定,则默认返回YYYY-MM-DD HH:MM:SS
}
注意事项:无
**/
export function formatDateTime(timestamp,formatType) {
let returnData,time = timestamp ? new Date(timestamp.replace(/\.|\-/g, '/')) : new Date();
let year = time.getFullYear()
const month = (time.getMonth() + 1).toString().padStart(2, '0')
const date = (time.getDate()).toString().padStart(2, '0')
const hours = (time.getHours()).toString().padStart(2, '0')
const minute = (time.getMinutes()).toString().padStart(2, '0')
const second = (time.getSeconds()).toString().padStart(2, '0')
switch(formatType){
case 'Numbers':
returnData = year + month + date + hours + minute + second;
break;
case 'YYYY-MM-DD':
returnData = year + '-' + month + '-' + date
break;
case 'HH:MM:SS':
returnData = hours + ':' + minute + ':' + second
break;
case 'MM-DD HH:MM':
returnData = month + '-' + date + ' ' + hours + ':' + minute
break;
case 'MM/DD':
returnData = month + '/' + date
break;
case '年月日时分秒':
returnData = year + '年' + month + '月' + date + '日 ' + hours + '时' + minute + '分' + second + '秒'
break;
default:
returnData = year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
break;
}
//console.log('[$formatDateTime] output',returnData)
return returnData;
}
根据开始日期计算到目前为止多少年月日时分秒
//返回结果看time判断都做了三元运算的
function timeDifference(startTime, endTime,type) {
// 传入起始时间 没有结束时间会自动获取 也做了开始时间的容错验证
// 有些时候没有开始时间 开始时间是默认当前时间
var stime;
if(!startTime){
stime = new Date().valueOf();
}else{
stime=Date.parse(new Date(startTime))
}
var etime;
if(!endTime){
etime = new Date().valueOf();
}else{
etime = Date.parse(new Date(endTime));
}
console.log(stime,etime)
// 此处是毫秒 下面除的是秒所有需要除以1000,因为计算问题做了小于等于 0判断
var runTime = (parseInt(etime - stime)/1000) <= 0? 0:(parseInt(etime - stime)/1000);
console.log(runTime)
if(runTime===0){
return 0 + '秒';
}
console.log(runTime)
var day = Math.floor(runTime / 86400)
runTime = runTime % 86400
var hour = Math.floor(runTime / 3600)
runTime = runTime % 3600
var minute = Math.floor(runTime / 60)
runTime = Math.floor(runTime % 60)
var second = runTime;
let time;
if(type=='day'){
time=day;
}else {
time= (day?day + "天":'')+ (hour?hour+'时':'')+ (minute?minute + "分":'') + (second?second+'秒':'');
}
return time;
}
获取今日、昨天、本周、本月、本季度、本年的起止时间范围
let params = {
startDate: dateType('startDate', '1'),
endDate: dateType('endDate', '1'),
type: '1',
}
function dateType(endOrdate, type) {
console.log(endOrdate,type)
let date = {
//昨天
yesterday: new Date(new Date().getTime() - 3600 * 1000 * 24).toLocaleDateString(),
//今天
today: new Date().toLocaleDateString(),
//本周一
monday: new Date(new Date().getTime() - (new Date().getDay() - 1) * 24 * 60 * 60 * 1000).toLocaleDateString(),
//本周日
sunday: new Date(new Date().getTime() + (7 - new Date().getDay()) * 24 * 60 * 60 * 1000).toLocaleDateString(),
//本月初
monthStart: new Date(new Date(new Date().getFullYear(), new Date().getMonth(), 1)).toLocaleDateString(),
//本月末
monthEnd: new Date(new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0)).toLocaleDateString(),
//本季初
quarterStart: getQuarter('start'),
//本季末
quarterEnd: getQuarter('end'),
//本年初
yearStart: new Date(new Date(new Date().getFullYear(), 0, 1)).toLocaleDateString(),
//本年末
yearEnd: new Date(new Date(new Date().getFullYear(), 12, 0)).toLocaleDateString(),
//7天前
days7ago: new Date(new Date().getTime() - 3600 * 1000 * 24 * 7).toLocaleDateString(),
//15天前
days15ago: new Date(new Date().getTime() - 3600 * 1000 * 24 * 15).toLocaleDateString(),
//30天前
days30ago: new Date(new Date().getTime() - 3600 * 1000 * 24 * 30).toLocaleDateString(),
//365天前
days365ago: new Date(new Date().getTime() - 3600 * 1000 * 24 * 365).toLocaleDateString(),
}
let returnData;
if(type=='1'){
if (endOrdate == 'startDate') {
returnData = date.today.split('/').join('-');
} else {
returnData = date.today.split('/').join('-');
}
}else if(type=='2'){
if (endOrdate == 'startDate') {
returnData = date.yesterday.split('/').join('-');
} else {
returnData = date.yesterday.split('/').join('-');
}
}else if(type=='3'){
if (endOrdate == 'startDate') {
returnData = date.monday.split('/').join('-');
} else {
returnData = date.sunday.split('/').join('-');
}
}else if(type=='4'){
if (endOrdate == 'startDate') {
returnData = date.monthStart.split('/').join('-');
} else {
returnData = date.monthEnd.split('/').join('-');
}
}else if(type=='5'){
if (endOrdate == 'startDate') {
returnData = date.quarterStart.split('/').join('-');
} else {
returnData = date.quarterEnd.split('/').join('-');
}
}else if(type=='6'){
if (endOrdate == 'startDate') {
returnData = date.yearStart.split('/').join('-');
} else {
returnData = date.yearEnd.split('/').join('-');
}
}else if(type=='7'){
if (endOrdate == 'startDate') {
returnData = date.days7ago.split('/').join('-');
} else {
returnData = date.today.split('/').join('-');
}
}else if(type=='8'){
if (endOrdate == 'startDate') {
returnData = date.days15ago.split('/').join('-');
} else {
returnData = date.today.split('/').join('-');
}
}else if(type=='9'){
if (endOrdate == 'startDate') {
returnData = date.days30ago.split('/').join('-');
} else {
returnData = date.today.split('/').join('-');
}
}
return returnData
}
// 获取季度时间
function getQuarter(type) {
let currentMonth = new Date().getMonth(); //当前月
let quarterStartMonth = 0;
if (currentMonth < 3) {
quarterStartMonth = 0;
}
if (2 < currentMonth && currentMonth < 6) {
quarterStartMonth = 3;
}
if (5 < currentMonth && currentMonth < 9) {
quarterStartMonth = 6;
}
if (currentMonth > 8) {
quarterStartMonth = 9;
}
if (type == 'start') {
return new Date(new Date(new Date().getFullYear(), quarterStartMonth, 1)).toLocaleDateString()
}
if (type == 'end') {
return new Date(new Date(new Date().getFullYear(), quarterStartMonth + 3, 0)).toLocaleDateString()
}
}
本月第一天,本月最后一天(yyyy-mm-dd)
//时间格式都是yyyy-mm-dd
//获取本月的开始时间
function currentTime() {
const currentDate = new Date();
const year = currentDate.getFullYear();//获取当前年
const month = String(currentDate.getMonth() + 1).padStart(2, "0");//获取当前月
const firstDay = "01";//日
return `${year}-${month}-${firstDay}`;
}
// 获取本月的结束时间
function getEndOfMonth() {
const now = new Date();
const nextMonth = now.getMonth() + 1;
const year = now.getFullYear();
if (nextMonth === 13) {
return new Date(year + 1, 0, 0).toISOString().split('T')[0];
} else {
return new Date(year, nextMonth, 0).toISOString().split('T')[0];
}
}
今天、7天前、30天前
//变量名是我逻辑中用到的,根据自己的实际代码逻辑进行更改
let timer = ['', ''];
if (state.alarmNuminfo.timeHorizon == '1') {
let startDate = getDay(0);
let endDate = getDay(1);
timer = [startDate, endDate];
} else if (state.alarmNuminfo.timeHorizon == '2') {
let startDate = getDay(-7);
let endDate = getDay(0)
timer = [startDate, endDate];
} else if (state.alarmNuminfo.timeHorizon == '3') {
let startDate = getDay(-30);
let endDate = getDay(0)
timer = [startDate, endDate];
}
let params = {
startDate: timer[0],
endDate: timer[1]
}
function getDay(day) {
var today = new Date();
var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
today.setTime(targetday_milliseconds); //注意,这行是关键代码
var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = doHandleMonth(tMonth + 1);
tDate = doHandleMonth(tDate);
return tYear + "-" + tMonth + "-" + tDate;
}
//获取当前月份
function doHandleMonth(month) {
var m = month;
if (month.toString().length == 1) {
m = "0" + month;
}
return m;
}
将秒数格式化为易读格式
/**
函数名称:将秒数格式化为易读格式
函数作者:不实名同事
参数说明:{
s: Number 秒数
}
返回说明: String 格式化后的时间, 最大单位为天
注意事项:例secondsFormat(3665) 输出 '1小时01分05秒'
**/
export function secondsFormat(s) {
if (!(/(^[1-9]\d*$)/.test(s))){
return '参数格式错误'
}
else{
var year = Math.floor(s / (365 * 24 * 3600)); // 计算年数
var month = Math.floor((s % (365 * 24 * 3600)) / (30 * 24 * 3600)); // 计算月数
var day = Math.floor((s % (30 * 24 * 3600)) / (24 * 3600)); // 计算剩余的天数
var hour = Math.floor((s % (24 * 3600)) / 3600); // 计算小时数
var minute = Math.floor((s % 3600) / 60); // 计算分钟数
var second = s % 60; // 计算剩余的秒数
let str = '';
if (year) str += year + "年";
if (month) str += month + "月";
if (day) str += day + "天";
if (hour) str += hour + "小时";
str += (minute > 9 ? minute : "0" + minute) + "分";
str += (second > 9 ? second : "0" + second) + "秒";
return str;
}
}
获取本周开始时间和本周结束时间
/**
函数名称:本周开始时间到本周结束时间
函数作者: 诺小D
参数说明:{ 不用传参}
注意事项:返回的是一个对象{start:'YYYY-MM-DD HH:mm:ss',end:'YYYY-MM-DD HH:mm:ss'}
**/
export function getWeekStartAndEnd() {
const now = new Date();
const dayOfWeek = now.getDay();
const dayOffset = dayOfWeek - 1;
const start = new Date(now);
start.setDate(start.getDate() - dayOffset);
start.setHours(0, 0, 0, 0); // 设置时间为00:00:00
const end = new Date(start);
end.setDate(end.getDate() + 6);
end.setHours(23, 59, 59, 999); // 设置时间为23:59:59
//此处是一个时间戳转日期方法 可以单独使用
function timestampToDate(timestamp) {
const date = new Date(timestamp); // 如果timestamp是数字,直接用于创建Date对象
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的
const day = ('0' + date.getDate()).slice(-2);
const hours = ('0' + date.getHours()).slice(-2);
const minutes = ('0' + date.getMinutes()).slice(-2);
const seconds = ('0' + date.getSeconds()).slice(-2);
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
return {
start:timestampToDate(start) ,
end: timestampToDate(end)
};
}
获取指定年份和月份的当月日期列表
/**
函数名称:获取指定年份和月份的当月日期列表
函数作者:不实名同事
参数说明:{
timespan: String 时间跨度('week'代表周,'month'代表月,'year'代表年,也支持指定年或年月,如‘2024’,'2024-05')
returnType:String 返回日期类型(支持以下格式: YYYY-MM-DD / MM/DD, 若未指定,则默认返回YYYY-MM-DD)
}
返回说明: Array 以数组形式返回指定年份和月份的当月日期列表,数组中的每个元素表示一个日期
**/
export function getDays(span, dateFormat = 'YYYY-MM-DD') {
const currentDate = new Date();
let startDate, endDate;
// 根据span参数确定起始和结束日期
switch (span.toLowerCase()) {
case 'week':
// 获取本周第一天(假设为周日)
startDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() - currentDate.getDay() + (currentDate.getDay() === 0 ? -6 : 1));
// 获取本周最后一天(周六)
endDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() + (6 - currentDate.getDay()));
break;
case 'month':
// 获取本月第一天
startDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
// 获取本月最后一天
endDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0);
break;
case 'year':
// 获取本年第一天
startDate = new Date(currentDate.getFullYear(), 0, 1);
// 获取本年最后一天
endDate = new Date(currentDate.getFullYear(), 11, 31);
break;
default:
// 尝试解析为指定年份或年月
const parts = span.split('-');
if (parts.length === 1) {
// 指定年份
startDate = new Date(parseInt(parts[0]), 0, 1);
endDate = new Date(parseInt(parts[0]), 11, 31);
} else if (parts.length === 2) {
// 指定年月
startDate = new Date(parseInt(parts[0]), parseInt(parts[1]) - 1, 1);
endDate = new Date(parseInt(parts[0]), parseInt(parts[1]), 0);
endDate.setDate(endDate.getDate() + 1); // 修正为当月最后一天
} else {
throw new Error('无法解析时间区域参数');
}
}
// 创建一个数组来保存日期
const dates = [];
// 使用循环填充日期数组
let current = new Date(startDate);
while (current <= endDate) {
// 根据日期格式生成字符串
let formattedDate;
if (dateFormat === 'YYYY-MM-DD') {
formattedDate = `${current.getFullYear()}-${('0' + (current.getMonth() + 1)).slice(-2)}-${('0' + current.getDate()).slice(-2)}`;
} else if (dateFormat === 'MM/DD') {
formattedDate = `${('0' + (current.getMonth() + 1)).slice(-2)}/${('0' + current.getDate()).slice(-2)}`;
} else {
throw new Error('Invalid date format');
}
dates.push(formattedDate);
current.setDate(current.getDate() + 1);
}
return dates;
}
分页序号回显
/**
函数名称:分页序号回显
函数作者: 诺小D
参数说明:{
pageNo:String 分页的页码
pageSize:pageSize 分页的条数
index:此数据在分页中的下标,
}
注意事项:无
**/
export function pageSort(pageNo,pageSize,index) {
return (pageNo - 1) * pageSize + index + 1
}
后续有再加