freight-web/src/utils/index.ts

131 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-03-12 05:34:36 +00:00
export function formatDate(time: any, cFormat: string) {
const format = cFormat || "{y}-{m}-{d}";
const date = new Date(time);
2024-03-20 02:43:46 +00:00
const formatObj: any = {
2024-03-12 05:34:36 +00:00
//年
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;
}
2024-03-15 02:40:59 +00:00
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 {
2024-03-20 02:43:46 +00:00
start: currentMonthStart,
end: currentMonthEnd,
2024-03-15 02:40:59 +00:00
};
}
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 {
2024-03-20 02:43:46 +00:00
start: currentYearStart,
end: currentYearEnd,
2024-03-15 02:40:59 +00:00
};
}
2024-03-19 08:49:30 +00:00
export function deleteBaseKey(obj: any) {
delete obj.createTime;
delete obj.createUserId;
delete obj.createUserName;
delete obj.updateTime;
delete obj.updateUserId;
delete obj.updateUserName;
2024-03-20 02:43:46 +00:00
return obj;
}
export function moneyFormat(num: number, decimal = 2, split = ",") {
/*
parameter
num
decimal2
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,
};
2024-03-19 08:49:30 +00:00
}