import moment from 'moment';
const format = 'HH:mm';

//2020/7/14/18:09:00
export const timestampToTime2 = (timestamp: any) => {
  var date = new Date(timestamp); //*1000//getHours getMinutes getSeconds

  return (
    date.getFullYear() +
    '/' +
    (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) +
    ' ' +
    date.getDate()
  );
  // return date.getFullYear()+'/' + (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + ' '+date.getDate()+' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds()
};

//月份补零
function addTZero(time) {
  let tmp = time + 1 < 10 ? '0' + (time + 1) : time + 1;
  return tmp;
}
function addZero(time) {
  let tmp = time + 1 < 10 ? '0' + time : time;
  return tmp;
}
const month = 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec'.split(',');
const MonthTurnNum = (values: any) => {
  var i = 0;
  month.map((item, index) => {
    if (values == item) {
      i = index + 1;
    }
  });
  return i;
};
export const getStringToTime = (time: any) => {
  var date = time.split(' ');
  var result =
    addTZero(MonthTurnNum(date[0])) +
    '-' +
    addZero(parseInt(date[1].slice(0, date[1].length - 1))) +
    ' ' +
    date[2];
  return result;
};
//03-08-2020 13:26
export const timestampToTime4 = (timestamp: any) => {
  var date = new Date(timestamp); //*1000//getHours getMinutes getSeconds

  return (
    addZero(date.getDate()) +
    '-' +
    (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) +
    '-' +
    date.getFullYear() +
    ' ' +
    addZero(date.getHours()) +
    ':' +
    addZero(date.getMinutes())
  );
};
// 03-06 13:26
export const timestampToTime3 = (timestamp: any) => {
  var date = new Date(timestamp); //*1000//getHours getMinutes getSeconds

  return (
    addZero(date.getDate()) +
    '-' +
    addTZero(date.getMonth()) +
    ' ' +
    addZero(date.getHours()) +
    ':' +
    addZero(date.getMinutes())
  );
};

// 	03-08-2020
export const timestampToTime = (timestamp: any) => {
  var date = new Date(timestamp); //*1000//getHours getMinutes getSeconds

  return (
    addZero(date.getDate()) +
    '-' +
    (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) +
    '-' +
    date.getFullYear()
  );
};

//时间转换
// 时间的拆分 var tmp = ["10:00-12:00", "13:00-14:00"]
export const timeToMoment = (tmp: any) => {
  if (tmp == null) return null;
  // var result = new Array()
  // tmp.map((item:any, index:any) => {
  //   var piece = item.split('-')
  //   result.push([moment(piece[0], format), moment(piece[1], format)])
  // })
  // return result;
  var piece = tmp.split('-');
  return [moment(piece[0], format), moment(piece[1], format)];
};

export const MomentToTime = (tmp: any) => {
  if (tmp == null) return null;

  return tmp.format('YYYY-MM-DD HH:mm:ss');
};
export const TimeToMoment2 = (tmp: any) => {
  if (tmp == null) return null;

  return moment(tmp, 'YYYY-MM-DD HH:mm:ss');
};
