Files
irc_web/src/utils/formatter.js
T
熊飞 3e3e54ed8f 1
2023-12-25 13:56:19 +08:00

153 lines
5.0 KiB
JavaScript

// eslint-disable-next-line no-extend-native
Date.prototype.format = function(fmt) {
var o = {
'M+': this.getMonth() + 1, // 月份
'd+': this.getDate(), // 日
'h+': this.getHours(), // 小时
'm+': this.getMinutes(), // 分
's+': this.getSeconds(), // 秒
'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
S: this.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)) }
}
return fmt
}
export function formatUTCTime(utc) {
if (utc !== '0001-01-01T00:00:00Z') {
const date = new Date(utc)
// 格式化时间
return FormatTime(date, 'yyyy-MM-dd')
} else {
return ''
}
}
export function fmtDate(obj) {
var date = new Date(obj)
var y = 1900 + date.getYear()
var m = '0' + (date.getMonth() + 1)
var d = '0' + date.getDate()
return y + '-' + m.substring(m.length - 2, m.length) + '-' + d.substring(d.length - 2, d.length)
}
export function parseISOLocal(s) {
var b = s.split(/\D/)
return new Date(b[0], b[1] - 1, b[2])
}
export function FormatTime(date, fmt) {
const o = {
'y+': date.getFullYear(),
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), // 日
'h+': date.getHours(), // 小时
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
'S+': date.getMilliseconds() // 毫秒
}
for (const k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
if (k === 'y+') {
fmt = fmt.replace(RegExp.$1, ('' + o[k]).substr(4 - RegExp.$1.length))
} else if (k === 'S+') {
let lens = RegExp.$1.length
lens = lens === 1 ? 3 : lens
fmt = fmt.replace(RegExp.$1, ('00' + o[k]).substr(('' + o[k]).length - 1, lens))
} else {
fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
}
}
}
return fmt
}
export function fn(time) {
var date = new Date(time)
var len = time.toString().length
// 时间戳不足13位则在后面加零
if (len < 13) {
var sub = 13 - len
sub = Math.pow(10, sub)
date = new Date(time * sub)
}
var y = date.getFullYear() + '-'
var M = date.getMonth() + 1
M = (M < 10 ? '0' + M : M) + '-'
var d = date.getDate()
d = (d < 10 ? '0' + d : d) + ' '
var h = date.getHours()
h = (h < 10 ? '0' + h : h) + ':'
var m = date.getMinutes()
m = (m < 10 ? '0' + m : m) + ':'
var s = date.getSeconds()
s = s < 10 ? '0' + s : s
return y + M + d + h + m + s
}
export function getLast3Month() {
var now = new Date()
var year = now.getFullYear()
var month = now.getMonth() + 1 // 0-11表示1-12月
var day = now.getDate()
var dateObj = {}
if (parseInt(month) < 10) {
month = '0' + month
}
if (parseInt(day) < 10) {
day = '0' + day
}
dateObj.now = year + '-' + month
if (parseInt(month) === 1) {
// 如果是1月份,则取上一年的10月份
dateObj.last = parseInt(year) - 1 + '-10'
return dateObj
}
if (parseInt(month) === 2) {
// 如果是2月份,则取上一年的11月份
dateObj.last = parseInt(year) - 1 + '-11'
return dateObj
}
if (parseInt(month) === 3) {
// 如果是3月份,则取上一年的12月份
dateObj.last = parseInt(year) - 1 + '-12'
return dateObj
} else {
dateObj.last = year + '-0' + (parseInt(month) - 3)
return dateObj
}
}
export function num2Money(num, cent, isThousand) {
if (num == null) return '0.0'
num = num.toString().replace(/\$|\,/g, '')
// 检查传入数值为数值类型
if (isNaN(num)) num = '0'
// 获取符号(正/负数)
var sign = num === (num = Math.abs(num))
num = Math.floor(num * Math.pow(10, cent) + 0.50000000001) // 把指定的小数位先转换成整数.多余的小数位四舍五入
var cents = num % Math.pow(10, cent) // 求出小数位数值
num = Math.floor(num / Math.pow(10, cent)).toString() // 求出整数位数值
cents = cents.toString() // 把小数位转换成字符串,以便求小数位长度
// 补足小数位到指定的位数
while (cents.length < cent) cents = '0' + cents
if (isThousand) {
// 对整数部分进行千分位格式化.
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) { num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3)) }
}
if (cent > 0) return (sign ? '' : '-') + num + '.' + cents
// return Number( (((sign) ? '' : '-') + num + '.' + cents))
else return (sign ? '' : '-') + num
// return Number((((sign) ? '' : '-') + num))
}
export function fomatFloat(src, pos) {
return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos)
}