毒牙的代码世界:Javascript的一些方法总结(面试 + 工作 + 日常总结)

版权声明:毒牙工作室 https://blog.csdn.net/qq_35023116/article/details/81457225

Javascript的一些方法总结  ---->毒牙

1、正则匹配电话格式

if(!/(^1[3|5|7|8][0-9]{9}$)/.test(你要验证的参数)) {
      // TODO
 }

2、解决toFixed()的bug

(Math.round(param* 100) / 100).toFixed(2) 

3 金钱千位分钱符号 + 保留两位小数

/*
 * @Author: 毒牙  
 * @Last Modified by: 毒牙
 * @desc 金钱符号转换 && 保留两位小数(四舍五入)
 */
function transformatMoney(money) {
  money = new Number(money) ? (Math.round(money * 100) / 100).toFixed(2) : "0.00";
  return money.toString().replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
    return $1 + ",";
  });
}

4、封装时间格式

const FormatDate = {

  //转换为yyyy-dd-mm
  dateFormat(timestamp) {
    timestamp = typeof timestamp === "number" ? timestamp : parseInt(timestamp)
    let time = new Date(timestamp);
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let date = time.getDate();
    return year + '-' + addZero(month) + '-' + addZero(date);
  },

  //转换为yyyy-MM-dd HH-mm-ss
  timeFormat(timestamp) {
    timestamp = typeof timestamp === "number" ? timestamp : parseInt(timestamp)
    let time = new Date(timestamp);
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let date = time.getDate();
    let hours = time.getHours();
    let minutes = time.getMinutes();
    let seconds = time.getSeconds();
    return year + '-' + addZero(month) + '-' + addZero(date) + ' ' + addZero(hours) + ':' + addZero(minutes) + ':' + addZero(seconds);
  }

}

// 补0
function addZero(m) {
  return m < 10 ? '0' + m : m
}



猜你喜欢

转载自blog.csdn.net/qq_35023116/article/details/81457225