zoukankan      html  css  js  c++  java
  • Javascript 常用的工具函数,更新中...

    1、时间戳转为格式化时间

    /**
     * 时间戳转为格式化时间
     * @Author   chenjun
     * @DateTime 2017-11-10
     * @param    {[date]}   timestamp [时间戳]
     * @param    {[string]}   formats   [时间格式]
     */
    function formatDate(timestamp, formats) {
        /*
        formats格式包括
        1. Y-M-D
        2. Y-M-D h:m:s
        3. Y年M月D日
        4. Y年M月D日 h时m分
        5. Y年M月D日 h时m分s秒
        示例:console.log(formatDate(1500305226034, 'Y年M月D日 h:m:s')) ==> 2017年07月17日 23:27:06
         */
        formats = formats || 'Y-M-D';
    
        var myDate = timestamp ? new Date(timestamp) : new Date();
    
        var year = myDate.getFullYear();
        var month = formatDigit(myDate.getMonth() + 1);
        var day = formatDigit(myDate.getDate());
    
        var hour = formatDigit(myDate.getHours());
        var minute = formatDigit(myDate.getMinutes());
        var second = formatDigit(myDate.getSeconds());
    
        return formats.replace(/Y|M|D|h|m|s/g, function(matches) {
            return ({
                Y: year,
                M: month,
                D: day,
                h: hour,
                m: minute,
                s: second
            })[matches];
        });
        // 小于10补0
        function formatDigit(n) {
            return n.toString().replace(/^(d)$/, '0$1');
        };
    }
    

    2、千分位显示,常用于价格显示:

    // 千分位
    function toThousands(num) {
    	return parseFloat(num).toFixed(2).replace(/(d{1,3})(?=(d{3})+(?:.))/g, "$1,");
    }
    

    3、超出显示省略号

    // 超出显示省略号
    function cutString(str, len) {
        //length属性读出来的汉字长度为1
        if (str.length * 2 <= len) {
            return str;
        }
        var strlen = 0;
        var s = "";
        for (var i = 0; i < str.length; i++) {
            s = s + str.charAt(i);
            if (str.charCodeAt(i) > 128) {
                strlen = strlen + 2;
                if (strlen >= len) {
                    return s.substring(0, s.length - 1) + "...";
                }
            } else {
                strlen = strlen + 1;
                if (strlen >= len) {
                    return s.substring(0, s.length - 2) + "...";
                }
            }
        }
        return s;
    }
    

    4、生成随机整数

    // 生成随机整数
    function random(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    

    5、生成uuid

    function getuuid() {
        var s = [];
        var hexDigits = "0123456789abcdef";
        for (var i = 0; i < 36; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = "-";
    
        var uuid = s.join("");
        return uuid;
    }
    

    6、获取url参数,兼容hash后的参数(可获取中文参数)

    function getQueryString(name) {
        let search = window.location.search.substr(1) || window.location.hash.split('?')[1];
        let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        if (!search) return;
        let r = search.match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }
    
  • 相关阅读:
    安装elasticsearch 和 kibana
    docker 安装 nignx 并将对应配置文件映射
    linux 操作笔记
    docker 一些常用的命令手记
    c# 异步 多线程
    从零开始在.net中使用Nhibernate对数据库进行操作详细步骤(20121128)
    NHibernate框架的HQL增删改查
    2012年11月19日 利用wifiap简单实现Wifi无线Web认证
    逻辑结构和物理结构
    日志
  • 原文地址:https://www.cnblogs.com/jone-chen/p/7815912.html
Copyright © 2011-2022 走看看