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}`
}
const Background_base64 = path => {
  return 'data:image/png;base64,' + wx.getFileSystemManager().readFileSync(path, 'base64');

}
const getdataTime = n => {
// 引入moment.js库
const moment = require('moment');
// 获取当前时间
let currentTime = moment();
  if (n==7) {
// 获取7天前的时间
let sevenDaysAgo = moment().subtract(7, 'days');
// return sevenDaysAgo.format('YYYY-MM-DD HH:mm:ss');
return sevenDaysAgo.format('YYYY-MM-DD');
  }else if (n==31) {
   // 获取30天前的时间
let thirtyDaysAgo = moment().subtract(30, 'days');
return thirtyDaysAgo.format('YYYY-MM-DD');
  }else if (n==365){
// 获取当前年份的开始时间
let startOfYear = moment().subtract(365, 'days');
return startOfYear.format('YYYY-MM-DD');
 
  }else if(n=='当日'){
// 格式化当前时间为 YYYY-MM-DD
return currentTime.format('YYYY-MM-DD');

  }

}
const CumulativeTime = n => {
  // 引入moment.js库
const moment = require('moment');
// 获取7天前的时间
let sevenDaysAgo = moment().subtract(n, 'days');
// return sevenDaysAgo.format('YYYY-MM-DD HH:mm:ss');
return sevenDaysAgo.format('YYYY-MM-DD');
}

module.exports = {
  formatTime,
  Background_base64,
  getdataTime,
  CumulativeTime
}
