/**
 *格式化时间
 *yyyy-MM-dd hh:mm:ss
 */
export function formatDate(time, fmt) {
  if (time === undefined || time === '' || time === null || time === 0) {
    return
  }
  if (typeof time === 'string') {
    return time
  }
  time = time * 1000
  const date = new Date(time)
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  const o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (const k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      const str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
    }
  }
  return fmt
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length)
}

export function timestampFormatDate(timestamp, format) {
  let date = timestampFormatDate(timestamp);
  let result = formatDate(date, format)
  return result
}

var minute = 60;
var hour = minute * 60;
var day = hour * 24;
var week = day * 7;
var month = day * 30;

export function getTimer(stringTime,language) {
  var time1 = new Date().getTime() / 1000;//当前的时间戳
  var time2 = stringTime; //Date.parse(new Date(stringTime));//指定时间的时间戳
  var time = time1 - time2;
  var result = null;
  /*if(time < 0){

  }else if(time/month >= 1){
      result = parseInt(time/month) + "月前";
  }else if(time/week >= 1){
      result =  parseInt(time/week) + "周前";
  }else */
  if (time / day >= 30) {
    result = language === 2 ? "30天前" : '30 days ago ';
  } else if (time / day >= 1) {
    result = language === 2 ? parseInt(time / day) + "天前" : parseInt(time / day) + 'days ago';
  } else if (time / hour >= 1) {
    result = language === 2 ? parseInt(time / hour) + "小时前" : parseInt(time / hour) + 'hours ago';
  } else if (time / minute >= 1) {
    result = language === 2 ? parseInt(time / minute) + "分钟前" : parseInt(time / minute) + 'minutes ago';
  } else {
    //  result = "刚刚";
    result = language === 2 ? "1分钟前" : '1 minutes age';
  }
  return result;
}




