zoukankan      html  css  js  c++  java
  • js常用工具函数

    js常用工具函数

    • (多为项目中需要,然后收集出来的,有一些根据自己项目需求做了修改,非全部原创,只为学习与记录)
      1 // 数值千分化例如1000000转化为1,000,000,其中对小数做了处理,对含小数的多位数值友好。
      2   function toThousands(num) {
      3     var num = (num || 0).toString(), result = '';
      4     let num1 = ""
      5     let num2 = ""
      6     if(num.indexOf(".") >= 0) {
      7       num1 = num.split(".")[0]
      8       num2 = num.split(".")[1]
      9       
     10     } else {
     11       num1 = num
     12       num2 = "" 
     13     }
     14     while (num1.length > 3) {
     15         result = ',' + num1.slice(-3) + result;
     16         num1 = num1.slice(0, num1.length - 3);
     17     }
     18     if (num1 && num2) { result = num1 + result + "." + num2; }
     19     else {result = num1 + result}
     20     return result;
     21   }
     22   // 复制新对象
     23   function extendCopy(p)  { 
     24     var c = {};  
     25     for (var i in p) {    
     26         c[i] = p[i];  
     27     }  
     28     return c;
     29   }
     30 
     31   const compare = property => {
     32     return function (a, b) {
     33       if(a[property] > 0){
     34         return a[property]
     35       }else {
     36         return b[property]
     37       }
     38     }
     39   }
     40   // 处理域名
     41   function geturl (){
     42     var origin = location.origin;
     43     var url = 'https://sit.tohours.com/businessplanner/';
     44     if(origin.indexOf('p.tohours.com') >= 0 || origin.indexOf('svn.tohours.com') >= 0 || origin.indexOf('sit.tohours.com') >= 0 || origin.indexOf('127.0.0.1:8020') >= 0){
     45         url = 'https://sit.tohours.com/businessplanner/';
     46     }else {
     47         url = origin + '/businessplanner/';
     48     }
     49     return url;
     50   }
     51   // 时间格式化函数2
     52   function parseTime(time, cFormat) {
     53     if (arguments.length === 0) {
     54         return null
     55     }
     56     const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
     57     let date
     58     if (typeof time === 'object') {
     59         date = time
     60     } else {
     61         if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
     62             time = parseInt(time)
     63         }
     64         if ((typeof time === 'number') && (time.toString().length === 10)) {
     65             time = time * 1000
     66         }
     67         date = new Date(time)
     68     }
     69     const formatObj = {
     70         y: date.getFullYear(),
     71         m: date.getMonth() + 1,
     72         d: date.getDate(),
     73         h: date.getHours(),
     74         i: date.getMinutes(),
     75         s: date.getSeconds(),
     76         a: date.getDay()
     77     }
     78     const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
     79     let value = formatObj[key]
     80     // Note: getDay() returns 0 on Sunday
     81     if (key === 'a') { 
     82         return ['日', '一', '二', '三', '四', '五', '六'][value ] 
     83     }
     84     if (result.length > 0 && value < 10) {
     85         value = '0' + value
     86     }
     87     return value || 0
     88     })
     89     return time_str
     90 }
     91   //时间格式化
     92   const formatTime = date => {
     93     const year = date.getFullYear()
     94     const month = date.getMonth() + 1
     95     const day = date.getDate()
     96     const hour = date.getHours()
     97     const minute = date.getMinutes()
     98     const second = date.getSeconds()
     99   
    100     return [year, month, day].map(formatNumber).join('-') 
    101     + ' ' + [hour, minute, second].map(formatNumber).join(':')
    102   }
    103   
    104   const formatNumber = n => {
    105     n = n.toString()
    106     return n[1] ? n : '0' + n
    107   }
    108   // 判断是否为number
    109   const isNumber = obj => {
    110     return !Number.isNaN(parseFloat(obj)) && Number.isFinite(Number(obj))
    111   }
    112   // 保留两位小数(只有一位小数时不补0)
    113   const getTwoPlaceNum = a => {
    114     return Math.round(a * 100) / 100
    115   } 
    116   // 保留两位小数(只有一位小数时补0)
    117   const getTwoPlaceNum2 = a => {
    118     var aNew;
    119     var re = /([0-9]+.[0-9]{2})[0-9]*/;
    120     aNew = a.replace(re,"$1");
    121     return aNew
    122   }
    123   
    124   export {
    125     toThousands,
    126     extendCopy,
    127     compare,
    128     formatTime,
    129     parseTime,
    130     isNumber,
    131     geturl,
    132     getTwoPlaceNum
    133   }
  • 相关阅读:
    2020面试阿里,字节跳动90%被问到的JVM面试题
    mysql数据库误删恢复
    因用了Insert into select语句,公司报警了
    Spring的Controller是单例还是多例?怎么保证并发的安全
    SpringBoot项目优化和Jvm调优
    java从一个pdf中取出指定页生成一个新的pdf
    java截取出字符串中的所有组数字
    oracle表空间位置迁移
    solr常用操作及集成分词器或cdh集群部署说明
    Oracle中将列查询结果多行逗号拼接成一个大字段
  • 原文地址:https://www.cnblogs.com/sinceForever/p/12667434.html
Copyright © 2011-2022 走看看