const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//获取时间
//获取d当前时间多少天后的日期和对应星期
function getDates(days, todate = new Date() ) { //todate默认参数是当前日期,可以传入对应时间
// function getDates(days,  todate =getCurrentMonthFirst()) { //todate默认参数是当前日期,可以传入对应时间
  var dateArry = [];
  for (var i = 0; i < days-3; i++) {
    var dateObj = dateLater(todate, i);
    console.log(dateObj)
    dateArry.push(dateObj)
  }
  return dateArry;
}
/**
 * 传入时间后几天
 * param:传入时间:dates:"2018-04-02",later:往后多少天
 */
function dateLater(dates, later) {
  let dateObj = {};
  let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  let date = new Date(dates);
  date.setDate(date.getDate() + later);
  let day = date.getDay();
  dateObj.year = date.getFullYear();
  dateObj.month = date.getMonth() + 1
  // dateObj.month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
  dateObj.day = date.getDate()
  // dateObj.day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  dateObj.week = show_day[day];
  return dateObj;
}
//获取页数
function onReachListData(page, pagenum, data, doFunction) {
  var that = page;
  var tiaoshu = data.length / pagenum;
  if (tiaoshu >= 10) {
    that.setData({
      pageNum: ++pagenum,
    });
    doFunction();
  } else {
    wx.showToast({
      title: '已加载全部',
      icon: 'none'
    })
  }
}
//获取登录信息
function getUser(msg) {
  wx.navigateTo({
    url: '/pages/index/index',
  })
}
module.exports = {
  formatTime: formatTime,
  onReachListData: onReachListData,
  getUser: getUser,
  getDates:getDates
}