js创建一段日期数据集合

 下面是获取前十五天和后十五天的日期

    let dateList = [];
    for (let i = -15; i < 15; i++) {
      let obj = getDateInfo(nowDateTime + i * 24 * 60 * 60 * 1000);
      obj.isChoose = i == 0;
      dateList.push(obj);
    }
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 getDateInfo = ts => {
  const date = new Date(ts);
  const weekArr = new Array("日", "一", "二", "三", "四", "五", "六");
  const week = date.getDay();
  let dateString = formatTime(date);
  let shortDateString = dateString.replace(/\//g, '-').substring(5, 10).replace(/-/g, '月') + "日";
  if (date.getDate() < 10) {
    shortDateString = shortDateString.replace(/0/g, '');
  }
  return {
    shortDateString,
    dateString,
    month: date.getMonth() + 1,
    day: date.getDate(),
    week: weekArr[week]
  }
}

 

你可能感兴趣的:(微信小程序开发)