const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 限制输入小数点两位
export const pointNum = function(obj) {
  obj = obj.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
  obj = obj.replace(/^\./g, ""); //验证第一个字符是数字
  obj = obj.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
  obj = obj.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
  obj = obj.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
  if (obj.indexOf(".") < 0 && obj != "") { //以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
    obj = parseFloat(obj);
  }
  if (!obj || obj == '0' || obj == '0.0' || obj == '0.00') {
    return;
  }
  return obj;
};

module.exports = {
  formatTime: formatTime,
  pointNum: pointNum
}