| | |
| | | * 最近 n 天的 startDate、endDate |
| | | * @param dates |
| | | */ |
| | | export const getRecentDateRange = (dates: number) => { |
| | | export const getRecentDateRange = (dates: number, includesCurrent = true) => { |
| | | dates = includesCurrent ? dates - 1 : dates; |
| | | // 获取当前日期 |
| | | const endDate = new Date(); |
| | | const startDate = new Date(); |
| | | startDate.setTime(startDate.getTime() - 3600 * 1000 * 24 * dates); |
| | | endDate.setHours(23,59,59,59) |
| | | startDate.setHours(0, 0, 0, 0); |
| | | return [startDate, endDate]; |
| | | }; |
| | |
| | | |
| | | export const toPercent = (num: number, havePercentSymbol = true, decimalPlaces = 1, defaultValue = '-') => { |
| | | if (num == null) return `${defaultValue} %`; |
| | | let percent = Number(num * 100).toFixed(decimalPlaces); |
| | | const factor = Math.pow(10, decimalPlaces); |
| | | |
| | | let percent = Math.round(Number(num) * 100 * factor) / factor + ''; |
| | | if (havePercentSymbol) { |
| | | percent += '%'; |
| | | } |
| | | return percent; |
| | | }; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 保留指定精度小数位,且不补零 |
| | | * @param num |
| | | * @param precision |
| | | * @returns |
| | | */ |
| | | export const toMyFixed = (num, precision) => { |
| | | if (num == null) return ''; |
| | | if (!precision) return num + ''; |
| | | const factor = Math.pow(10, precision); |
| | | return Math.round(Number(num) * factor) / factor + ''; |
| | | }; |
| | | |
| | | /** |
| | |
| | | }; |
| | | |
| | | export function getTextWidth(text: string, option: GetTextWidthOption) { |
| | | if (!text) return 0; |
| | | const { size = '14px', family = 'Microsoft YaHei' } = option; |
| | | const spanEle = document.createElement('span'); |
| | | document.body.appendChild(spanEle); |
| | |
| | | document.body.removeChild(spanEle); |
| | | return width; |
| | | } |
| | | |
| | | export function decodeFormData(formDataString) { |
| | | const params = new URLSearchParams(formDataString); |
| | | const decodedData = {}; |
| | | for (const [key, value] of params) { |
| | | decodedData[key] = decodeURIComponent(value); |
| | | } |
| | | return decodedData; |
| | | } |