freight-web/src/utils/index.ts

163 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { TimeRange } from "./enum";
export function formatDate(time: any, cFormat: string) {
const format = cFormat || "{y}-{m}-{d}";
const date = new Date(time);
const formatObj: any = {
//年
y: date.getFullYear(),
//月
m: date.getMonth() + 1,
//日
d: date.getDate(),
//小时
h: date.getHours(),
//分钟
i: date.getMinutes(),
//秒
s: date.getSeconds(),
//星期
a: date.getDay(),
};
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key];
// Note: getDay() returns 0 on Sunday
if (key === "a") {
//如果key是a就是星期格式化成一~日
//例如formatDate(new Date(), '{y}-{m}-{d}-{h}-{i}-{s}-{a}');
//会输出2021-10-29-00-00-00-五
//星期的value会返回0-6['日', '一', '二', '三', '四', '五', '六'][2]代表周二
return ["日", "一", "二", "三", "四", "五", "六"][value];
}
//padStart用于字符串头部补全2个字符如果不够前面补0
return value.toString().padStart(2, "0");
});
return time_str;
}
export function getCurrentMonthStartAndEnd() {
let now = new Date();
let currentMonthStart = new Date(now.getFullYear(), now.getMonth(), 1);
let currentMonthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0);
return {
start: currentMonthStart,
end: currentMonthEnd,
};
}
export function getCurrentYearStartAndEnd() {
let now = new Date();
let currentYearStart = new Date(now.getFullYear(), 0, 1); // 0 代表一月
let currentYearEnd = (new Date(now.getFullYear() + 1, 0, 1) as any) - 1; // 下一年的一月一日减一毫秒
return {
start: currentYearStart,
end: currentYearEnd,
};
}
export function deleteBaseKey(obj: any) {
delete obj.createTime;
delete obj.createUserId;
delete obj.createUserName;
delete obj.updateTime;
delete obj.updateUserId;
delete obj.updateUserName;
return obj;
}
export function moneyFormat(num: number, decimal = 2, split = ",") {
/*
parameter
num格式化目标数字
decimal保留几位小数默认2位
split千分位分隔符默认为,
moneyFormat(123456789.87654321, 2, ',') // 123,456,789.88
*/
if (isFinite(num)) {
// num是数字
if (num === 0) {
// 为0
return num.toFixed(decimal);
} else {
// 非0
var res = "";
var dotIndex = String(num).indexOf(".");
if (dotIndex === -1) {
// 整数
if (decimal === 0) {
res = String(num).replace(/(\d)(?=(?:\d{3})+$)/g, `$1${split}`);
} else {
res =
String(num).replace(/(\d)(?=(?:\d{3})+$)/g, `$1${split}`) +
"." +
"0".repeat(decimal);
}
} else {
// 非整数
// js四舍五入 Math.round()正数时4舍5入负数时5舍6入
// Math.round(1.5) = 2
// Math.round(-1.5) = -1
// Math.round(-1.6) = -2
// 保留decimals位小数
const numStr = String(
(
Math.round(num * Math.pow(10, decimal)) / Math.pow(10, decimal)
).toFixed(decimal)
); // 四舍五入然后固定保留2位小数
const decimals = numStr.slice(dotIndex, dotIndex + decimal + 1); // 截取小数位
res =
String(numStr.slice(0, dotIndex)).replace(
/(\d)(?=(?:\d{3})+$)/g,
`$1${split}`
) + decimals;
}
return res;
}
} else {
return "--";
}
}
// 分页内容重置
export function pageListInit() {
return {
noMoreData: false,
total: 0,
list: [],
pageNum: 1,
pageSize: 10,
};
}
// 过滤掉属性值为null的
export function filterNullUndefined(obj: Object) {
return Object.entries(obj).reduce((acc: any, [key, value]) => {
if (value !== null && value !== undefined && value !== "") {
acc[key] = value;
}
return acc;
}, {});
}
export function timeRange(id: number) {
const today = new Date();
const yesterday = new Date((today as any) - 24 * 60 * 60 * 1000);
let startTime = "";
let endTime = "";
if (id === TimeRange.Today) {
startTime = formatDate(today, "{y}-{m}-{d}");
endTime = formatDate(today, "{y}-{m}-{d}");
} else if (id === TimeRange.Yesterday) {
startTime = formatDate(yesterday, "{y}-{m}-{d}");
endTime = formatDate(yesterday, "{y}-{m}-{d}");
} else if (id === TimeRange.Month) {
startTime = formatDate(getCurrentMonthStartAndEnd().start, "{y}-{m}-{d}");
endTime = formatDate(getCurrentMonthStartAndEnd().end, "{y}-{m}-{d}");
} else if (id === TimeRange.Year) {
startTime = formatDate(getCurrentYearStartAndEnd().start, "{y}-{m}-{d}");
endTime = formatDate(getCurrentYearStartAndEnd().end, "{y}-{m}-{d}");
}
return { startTime: startTime, endTime: endTime };
}