let TimeUtil = {
  //console.log(formatDateTime(1495157126));
  formatDateTime(timeStamp) {
    var date = new Date();
    date.setTime(timeStamp);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes()
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  },
  formatDateTimeOfYMD(timeStamp) {
    var date = new Date();
    date.setTime(timeStamp);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;

    return y + '-' + m + '-' + d;
  },

  /**
   * 格式化时间戳
   * @param {Number} timeStamp 时间戳，毫秒
   * @param {string} format 格式，Date/DateTime/DateTimeSecond
   */
  formatTimeStamp(timeStamp, format) {
    var date = new Date();
    date.setTime(timeStamp);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes()
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;

    if (format === 'Date') {
      return y + '-' + m + '-' + d
    } else if (format === 'DateTime') {
      return y + '-' + m + '-' + d + ' ' + h + ':' + minute
    } else if (format === 'DateTimeSecond') {
      return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
    } else {
      return y + '-' + m + '-' + d
    }
  },

  /**
   * 格式化时间戳
   * @param {Number} timeStamp 时间戳，毫秒
   * @param {string} format 格式，Date/DateTime/DateTimeSecond
   */
  formatTimeStampInChinese(timeStamp, format) {
    var translateDate = new Date(timeStamp);
    let minusSecond = translateDate.getTimezoneOffset()*60*1000;
    let timeStampNow = translateDate.getTime() + minusSecond;
    var date = new Date(timeStampNow);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes()
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;

    if (format === 'Date') {
      return y + '年' + m + '月' + d + '日'
    } else if (format === 'DateTime') {
      return y + '年' + m + '月' + d + '日' + h + '时' + minute + '分'
    } else if (format === 'DateTimeSecond') {
      return y + '年' + m + '月' + d + '日' + h + '时' + minute + '分' + second + '秒'
    } else {
      return y + '年' + m + '月' + d + '日'
    }
  },

  /**
   * 以当天的23：59：59为截止时间，返回开始时间和结束时间
   * @param {Number} day 时间间隔，单位天
   * @returns {Object} {start: 1531152000000,end: 1531324800000}
   */
  timeInterval(day) {
    if (Object.prototype.toString.call(day) !== '[object Number]') {
      let err = new Error('输入参数错误')
      throw(err)
    }

    let today = new Date()
    let y = today.getFullYear()
    let m = today.getMonth()
    let d = today.getDate()

    /** 结束日期为当天的23：59：59 */
    let endDay =  new Date()
    endDay.setFullYear(y)
    endDay.setMonth(m)
    endDay.setDate(d)
    endDay.setHours(23)
    endDay.setMinutes(59)
    endDay.setSeconds(59)

    let msPerDay = 24 * 60 * 60 * 1000

    let startTimeStamp = (endDay.getTime() - (day * msPerDay) + 1000)
    return {start: startTimeStamp, end: endDay.getTime()}
  },
}
module.exports = TimeUtil
