js中封装一些常用小方法

oddsRide = (arg) => {
  if (!arg) return;
  /*****/
  if (arg.toString().indexOf('.') == 1) {
    if (arg.toString().split(".")[1].length > 3) {
      arg = parseFloat(arg).toFixed(2)
    }
  }
  let arg1 = 10;
  let m = 0, s1 = arg1.toString();
  let s2 = arg.toString();
  try {
    m += s1.split(".")[1].length
  } catch (e) {
  }
  try {
    m += s2.split(".")[1].length
  } catch (e) {
  }
  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
};

console.log(oddsRide(4555.278));//45552.78
getCount=(number)=>{
  var n = 2;
  var num = ""+number;
  if(num.substring(num.indexOf(".")<0?0:num.indexOf(".")).length<=n){
    return number;
  }

  n = n ? parseInt(n) : 0;
  if (n <= 0) return Math.round(number);
  number = Math.round(number * Math.pow(10, n)) / Math.pow(10, n);
  return number;
}

console.log(getCount(0.222));//0.22
normalTime = (time) => {
  if (time) {
    let oDate = new Date();
    oDate.setTime(time);

    let y = oDate.getFullYear();
    let m = oDate.getMonth() + 1;
    m = m < 10 ? '0' + m : m;
    let d = oDate.getDate();
    d = d < 10 ? '0' + d : d;

    let h = oDate.getHours();
    h = h < 10 ? '0' + h : h;
    let mm = oDate.getMinutes();
    mm = mm < 10 ? '0' + mm : mm;
    let s = oDate.getSeconds();
    s = s < 10 ? '0' + s : s;

    //return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + ' ' + h + ':' + mm + ':' + s;
    return `${y}-${m}-${d} ${h}:${mm}:${s}`;
  }
};
week=(date)=>{
  var date = new Date(date);
  var week, day;
  day = date.getDay();
  switch (day) {
    case 0:
      week = "星期日";
      break;
    case 1:
      week = "星期一";
      break;
    case 2:
      week = "星期二";
      break;
    case 3:
      week = "星期三";
      break;
    case 4:
      week = "星期四";
      break;
    case 5:
      week = "星期五";
      break;
    case 6:
      week = "星期六";
      break;
  }
  return week;
}

console.log(week('2018-11-1'));
/**
     * 获取url传过来的参数
     * @param name 参数名称
     * @param url URL地址
     * @param return
     */
    getQueryParam: function (name, url) {
        //URL GET 获取值
        var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i"),
            Url = url || location.href;
        if (reg.test(Url))
            return decodeURI(RegExp.$2.replace(/\+/g, " "));
        return "";

    },

 

你可能感兴趣的:(javascript)