javascript 常用的函数

判断a是不是数组
if (!Array.isArray) 
  Array.isArray = arg => Object.prototype.toString.call(arg) === '[object Array]';

Array.isArray(a)  //  true false
判断a是否是对象
const isObject = a => Object.prototype.toString.call(a) === "[object Object]" 
比较 a、b是否相等
function isObject (a) {
    const _ = Object.prototype.toString.call(a)
    return _ === "[object Object]" || _ === "[object Array]"
}
function isEqual (a, b) {
    if(a === b) return true
    let isObjectA = isObject(a), isObjectB = isObject(b);

    if (isObjectA && isObjectB) {
        try {
            let isArrayA = Array.isArray(a), isArrayB = Array.isArray(b);

            if (isArrayA && isArrayB)
                return a.length === b.length && a.every((e, i) => looseEqual(e, b[i]))
            else if (!isArrayA && !isArrayB) {
                let keysA = Object.keys(a), keysB = Object.keys(b);
                return keysA.length === keysB.length && keysA.every(key => looseEqual(a[key], b[key]))
            } 
            else return false
      
        } catch (e) {
            return false
        }
    } 
    else if (!isObjectA && !isObjectB) return String(a) === String(b)
    else return false
}
小写金额转成大写
function AmountNumToUpper (num) {
    if(typeof num != 'number' || Number(num) < 0 || isNaN(num)) 
        throw new Error('incorrect input number with' + num)

    let strUnit = '仟佰拾亿仟佰拾万仟佰拾元角分', strOutput = ""; num += "00";
    let intPos = num.indexOf('.');
    if (intPos >= 0)
        num = num.substring(0, intPos) + num.substr(intPos + 1, 2);
    strUnit = strUnit.substr(strUnit.length - num.length);
    for (var i=0; i < num.length; i++)
        strOutput += '零壹贰叁肆伍陆柒捌玖'.substr(num.substr(i,1),1) + strUnit.substr(i,1);
    return strOutput.
            replace(/零角零分$/, '整').
            replace(/零[仟佰拾]/g, '零').
            replace(/零{2,}/g, '零').
            replace(/零([亿|万])/g, '$1').
            replace(/零+元/, '元').
            replace(/亿零{0,3}万/, '亿').
            replace(/^元/, "零元");
}
美化日期时间
function formatDate (time, FormatStyle) {
  	if (arguments.length === 0) return null
  	const format = FormatStyle || '{y}-{m}-{d} {h}:{i}:{s}'
  	const date = typeof time === 'object'? time : new Date(time)
 	const formatObj = {
    	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(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    	let value = formatObj[key]
    	if (key === 'a') 
        	return '周' + ['日', '一', '二', '三', '四', '五', '六'][value]
    	return (result.length > 0 && value < 10)? '0' + value : value
  	})
  	return time_str
}
formatDate(new Date())  // "2019-08-15 20:08:55"
formatDate(new Date(),'{a}')  // "周四"
formatDate(new Date(),'{d}/{m}/{y} {h}:{i}:{s}')  // "15/08/2019 20:09:28"
计算发生时间据现在过了多久
function formatTime(time) {
    const date = typeof time === 'object'? time : new Date(time)
    const now = Date.now()
    const dura = (now - +date) / 1000
    if (dura < 60) {
        return Math.ceil(dura) + '秒前'
    } else if (dura < 3600) {
        return Math.floor(dura / 60) + '分钟前'
    } else if (dura < 3600 * 24) {
        return Math.floor(dura / 3600) + '小时前'
    } else if (dura / (3600 * 24) >= 1 && dura / (3600 * 24) < 8) {
        return Math.floor(dura / (3600 * 24)) + '天前'
    } else if (dura / (3600 * 24) >= 8) {
        return (
          date.getFullYear() + '/' +
          (date.getMonth() + 1) + '/' +
          date.getDate()
        )
    }
}

下面的图片是测试效果
调用示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值