美文网首页
微信小程序return日期+星期

微信小程序return日期+星期

作者: 理子 | 来源:发表于2019-01-05 12:09 被阅读15次
  1. 目录


    QQ截图20190105120135.png
  2. 在utils下新建date.js

// 作者:chibimarukochan
// 来源:CSDN
// 原文:https://blog.csdn.net/chibimarukochan/article/details/83750084
// 版权声明:本文为博主原创文章,转载请附上博文链接!


//得到时间格式2018-10-02
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')

}

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

//todate默认参数是当前日期,可以传入对应时间 todate格式为2018-10-05
function getDates(days, todate) {
  var dateArry = [];
  for (var i = 0; i < days; i++) {
    var dateObj = dateLater(todate, i);
    dateArry.push(dateObj)
  }
  return dateArry;
}

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();
  let yearDate = date.getFullYear();
  let month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
  let dayFormate = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  dateObj.time = yearDate + '-' + month + '-' + dayFormate;
  dateObj.week = show_day[day];
  return dateObj;
}

module.exports = {
  formatDate: formatDate,
  getDates: getDates
}
  1. 在x页面.js中引用date.js实现功能
//引用date.js
let sysdate = require('../../utils/date.js');

let time = sysdate.formatDate(new Date());
let date = sysdate.getDates(7, time);
console.log(date);

今天为2019.1.5日,结果:


QQ截图20190105120814.png

Rel:
https://blog.csdn.net/chibimarukochan/article/details/83750084
https://www.cnblogs.com/zzd0916/p/7723994.html
https://www.jianshu.com/p/33736c93d716
https://blog.csdn.net/qq_38912819/article/details/81564995
https://www.cnblogs.com/huangenai/p/6385342.html

相关文章

网友评论

      本文标题:微信小程序return日期+星期

      本文链接:https://www.haomeiwen.com/subject/twerrqtx.html