/* 全局过滤器 */

const  dateFormat = function (timeSpan, format) {
  if (!timeSpan) return;
  timeSpan = timeSpan.toString().length === 10 ? timeSpan * 1000 : timeSpan;
  let date = new Date(timeSpan);
  let o = {
    'M+': date.getMonth() + 1,
    'D+': date.getDate(),
    W: '日一二三四五六'.charAt(date.getDay()),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    S: date.getMilliseconds()
  };
  if (/(Y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
    }
  }
  return format;
}

/**
 * 时间戳---> 年-月-日 时:分:秒
 * @param timestamp
 */
const formatTimeStamp = function (data) {
  let date = new Date(data);
  let month = date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let newMonth = month < 10 ? '0' + month : month;
  let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return `${date.getFullYear()}-${newMonth}-${day} ${hours}:${minutes}:${seconds}`;
}

export default {
  dateFormat,
  formatTimeStamp
}
