zoukankan      html  css  js  c++  java
  • JS 实用方法

    //格式化数字
    function toStringNum(data,n,s){
        n = n||3;
        s = s||',';
        if(data){
            var dataArray = (data+'').split('');
            var show = "";
            for(var j=1,i=dataArray.length-1;i>=0;i--){
                show = dataArray[i]+show;
                if(j++%n == 0 && i>0){
                    show = s+show;
                }
            }
            return show;
        }
        return '';
    }
    toStringNum(dataAll.ajs.VALUE,3,',');//23,432
    //格式化日期
    function dateFormat(fmt, date) {
        let ret;
        let week = ['日','一','二','三','四','五','六']
        const opt = {
            "Y+": date.getFullYear().toString(),        //
            "m+": (date.getMonth() + 1).toString(),     //
            "d+": date.getDate().toString(),            //
            "H+": date.getHours().toString(),           //
            "M+": date.getMinutes().toString(),         //
            "S+": date.getSeconds().toString(),          //
            "w+": date.getDay().toString(),                //
            "W+": week[date.getDay()].toString()                //
            // 有其他格式化字符需求可以继续添加,必须转化成字符串
        };
        for (let k in opt) {
            ret = new RegExp("(" + k + ")").exec(fmt);
            if (ret) {
                fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
            };
        };
        return fmt;
    }
    dateFormat('YYYY年mm月dd日  星期W  HH:MM:SS',new Date());
    /**
     * String.padStart()
     * version 1.0.1
     * Feature            Chrome  Firefox Internet Explorer   Opera    Safari    Edge
     * Basic support    57       51      (No)                44       10      15
     * -------------------------------------------------------------------------------
     */
    if (!String.prototype.padStart) {
      String.prototype.padStart = function padStart(targetLength, padString) {
        targetLength = targetLength >> 0; //floor if number or convert non-number to 0;
        padString = String(typeof padString !== 'undefined' ? padString : ' ');
        if (this.length > targetLength) {
          return String(this);
        } else {
          targetLength = targetLength - this.length;
          if (targetLength > padString.length) {
            padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
          }
          return padString.slice(0, targetLength) + String(this);
        }
      };
    }
  • 相关阅读:
    49. 字母异位词分组
    73. 矩阵置零
    Razor语法问题(foreach里面嵌套if)
    多线程问题
    Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data
    How to execute tons of tasks parallelly with TPL method?
    How to sort the dictionary by the value field
    How to customize the console applicaton
    What is the difference for delete/truncate/drop
    How to call C/C++ sytle function from C# solution?
  • 原文地址:https://www.cnblogs.com/BambooLamp/p/13876998.html
Copyright © 2011-2022 走看看