日期转化

//数据转化  
function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}
/** 
 * 时间戳转化为年 月 日 时 分 秒 
 * number: 传入时间戳 
 * format:返回格式,支持自定义,但参数必须与formateArr里保持一致 
 */
function formatTime(number, format) {
    var format;
    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    var returnArr = [];
    if (!format) {
        // 是否是今天
        if (new Date(parseFloat(number) * 1000).toDateString() === new Date().toDateString()) {
            format = 'h:m'

        } else if (new Date(parseFloat(number) * 1000).getFullYear() === new Date().getFullYear()) {
            // 是否是今年
            format = 'M-D'
        } else {
            // 今年以前
            format = 'Y-M-D'
        }
    }

    var date = new Date(number * 1000);
    returnArr.push(date.getFullYear());
    returnArr.push(formatNumber(date.getMonth() + 1));
    returnArr.push(formatNumber(date.getDate()));
    returnArr.push(formatNumber(date.getHours()));
    returnArr.push(formatNumber(date.getMinutes()));
    returnArr.push(formatNumber(date.getSeconds()));

    for (var i in returnArr) {
        format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
}

将大数字格式化

/**
 * 将大数字格式化 例:N万 N亿 保留小数点后一位(方式为截取)
 * @param arg string/int 原始数据
 * @returns {*}
 */
function beautifyNumber(arg) {
    if (arg === undefined || arg === null || arg === "") {
        console.error("参数不能为空");
        return ""
    }
    var strArg = arg + "";
    var length = strArg.length;
    if (length <= 0) {
        return ""
    }
    if (length < 5) {
        return strArg
    }
    if (length < 6) {
        return strArg.substring(0, length - 4) + "." + strArg.substring(length - 4, length - 3) + "W"
    }
    if (length >= 6) {
        // return strArg.substring(0, length - 8) + "." + strArg.substring(length - 8, length - 7) + "亿"
        return "10W+"
    }
    return arg;
}

移动端自适应rem

    <script>
        /* rem自适应脚本
         * @param designWidth 设计稿页面宽度
         */
        ;
        (function (designWidth) {
            var docEle = document.documentElement;
            var scale = 1;
            var viewportContent =
                "width=device-width,initial-scale=" +
                scale +
                ",maximum-scale=" +
                scale +
                ",user-scalable=no";
            var meta = document.querySelector("meta[name=viewport]");
            if (meta) {
                meta.setAttribute("content", viewportContent);
            } else {
                meta = document.createElement("mata");
                meta.setAttribute("name", "viewport");
                meta.setAttribute("content", viewportContent);
                if (docEle.firstElementChild) {
                    //<head>
                    docEle.firstElementChild.appendChild(meta);
                } else {
                    var head = document.createElement("head");
                    head.autocapitalize(meta);
                    docEle.appendChild(head);
                }
            }
            //设置font-size
            docEle.style.fontSize = (docEle.clientWidth / designWidth) * 16 + "px";
        })(375);
    </script>


半亩花田
30 声望4 粉丝