/**
* 对日期进行格式化,
* @param date 要格式化的日期
* @param format 进行格式化的模式字符串
* 支持的模式字母有:
* y:年,
* M:年中的月份(1-12),
* d:月份中的天(1-31),
* h:小时(0-23),
* m:分(0-59),
* s:秒(0-59),
* S:毫秒(0-999),
* q:季度(1-4)
* @return String
*/
export const formatDate = (str, format) => {
if (!str) return ''
// console.log(str,fromat)
var date = new Date(str)
var map = {
'': 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() // 毫秒
}
format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
var v = map[t]
if (v !== undefined) {
if (all.length > 1) {
v = '0' + v
v = v.substr(v.length - 2)
}
return v
} else if (t === 'y') {
return (date.getFullYear() + '').substr(4 - all.length)
}
return all
})
return format + ''
}
/**
* 导出
*
* @export
* @param {*} stream 流格式
* @param {String} name 文件名
*/
export function tableExport (stream, name) {
const link = document.createElement('a')
let blob = new Blob([stream], {
type: 'application/vnd.ms-excel'
})
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
link.setAttribute('download', name + '.xlsx')
document.body.appendChild(link)
link.click()
URL.revokeObjectURL(link.href)
document.body.removeChild(link)
}
*
* 深拷贝
* @export
* @param {*} source 数据源
* @returns 返回备份数据
*/
export function deepCopy(source){
if (!isObject(source)) return source; //如果不是对象的话直接返回
let target = Array.isArray( source ) ? [] : {} //数组兼容
for ( var k in source ) {
if (source.hasOwnProperty(k)) {
if ( typeof source[ k ] === 'object' ) {
target[ k ] = deepCopy( source[ k ] )
} else {
target[ k ] = source[ k ]
}
}
}
return target
}
// 是否是对象
function isObject(obj) {
return typeof obj === 'object' && obj !== null
}
/**
* 获取当前是移动终端浏览器版本信息
* @returns {*} 返回移动终端浏览器版本信息
*/
export function browser () {
return {
versions: (function () {
var u = navigator.userAgent
return { // 移动终端浏览器版本信息
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, // android终端或uc浏览器
iPhone: u.indexOf('iPhone') > -1, // 是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') > -1 // 是否iPad
}
}())
}
}
/**
* js精度问题解决方案
* @export
* @param {any} f 算数表达式
* @param {any} digit 保留几位小数
* @returns 处理后的数据
*/
export function formatFloat (f, digit) {
digit = digit || 2
var m = Math.pow(10, digit)
return (parseFloat(f * m, 10) / m).toFixed(digit)
}
/**
* 将金额转换为1,234,567.89的格式
* @param {String} num 接受的过滤值
* @returns 处理后的数据
*/
export function ansFormatter (num) {
if (!num) return num
let parts = num.toString().split('.')
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
return parts.join('.')
}
export function getCookie (name) {
var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
var arr = document.cookie.match(reg)
if (arr) {
return unescape(arr[2])
} else { return null }
}
export function setCookie (name, value) {
var Days = 30
var exp = new Date()
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000)
document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString() + ';path=/'
}
常用工具(tools.js)类
最新推荐文章于 2024-03-25 09:52:46 发布