/**
 * 时间戳转换成时间
 * @param {*} time
 */
const formDate = (time, formate = "yyyy-mm-dd hh:mm:ss") => {
  var padDate = function (va) {
    va = va < 10 ? "0" + va : va;
    return va;
  };
  if (time) {
    var value = new Date(time * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    // var value = new Date(parseFloat(time))
    var year = value.getFullYear();
    var month = padDate(value.getMonth() + 1);
    var day = padDate(value.getDate());
    var hour = padDate(value.getHours());
    var minutes = padDate(value.getMinutes());
    var seconds = padDate(value.getSeconds());
    let res = "";
    switch (formate) {
      case "mm-dd": {
        res = month + "-" + day;
        break;
      }
      case "yyyy-mm-dd": {
        res = year + "-" + month + "-" + day;
        break;
      }
      case "yyyy-mm": {
        res = year + "-" + month;
        break;
      }
      case "mm月dd日": {
        res = month + "月" + day + "日";
        break;
      }
      case "yyyy年mm月dd日": {
        res = year + "年" + month + "月" + day + "日";
        break;
      }
      case "yyyy年mm月": {
        res = year + "年" + month + "月";
        break;
      }
      case "hh:mm": {
        res = hour + ":" + minutes;
        break;
      }
      case "yyyy-mm-dd hh:mm": {
        res = year + "-" + month + "-" + day + " " + hour + ":" + minutes;
        break;
      }
      case "yyyy.mm.dd":
        res = year + "." + month + "." + day;
        break;
      case "yyyy-mm-dd hh:mm:ss":
      default: {
        res =
          year +
          "-" +
          month +
          "-" +
          day +
          " " +
          hour +
          ":" +
          minutes +
          ":" +
          seconds;
        break;
      }
    }
    return res;
  }
  return "--";
};

const toFixed = (number) => {
  if (number > 0) {
    return number.toFixed(2);
  } else {
    return "0.00";
  }
};

export default {
  formDate,
  toFixed,
};
