import moment from 'moment'
export const date = (date, fmt = 'YYYY-MM-DD') => {
let str = date || ''
if (typeof str === 'string' && str && (str.includes('**') || str.includes('✽✽'))) {
return str
}
return str ? moment(str).format(fmt) : str
}
export const time = (d, fmt = 'YYYY-MM-DD HH:mm:ss') => {
return date(d, fmt)
}
export const minute = (d, fmt = 'YYYY-MM-DD HH:mm') => {
return date(d, fmt)
}
export const second = (d, fmt = 'HH:mm:ss') => {
return date(d, fmt)
}
export const month = (d, fmt = 'YYYY-MM') => {
return date(d, fmt)
}
export const money = (num, prev = '¥', fixed = 2) => {
if (typeof num !== 'number' && !num) {
return ''
}
if (typeof num === 'string' && (num.includes('**') || num.includes('✽✽'))) {
return num
}
prev = prev === null ? '¥' : prev
return `${prev}${(+num).toFixed(fixed).replace(/\B(?=(\d{3})+(?!\d))/g, ',')}`
}
export const decimal = (num, precision = 2) => {
if (typeof num !== 'number' && !num) {
return ''
}
if (typeof num === 'string' && (num.includes('**') || num.includes('✽✽'))) {
return num
}
return `${(+num).toFixed(precision).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')}`
}
export const thousands = (num) => {
if (typeof num !== 'number' && !num) {
return ''
}
if (typeof num === 'string' && (num.includes('**') || num.includes('✽✽'))) {
return num
}
return (+num).toLocaleString()
}