JavaScript获取往后天数带星期

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/Ruffaim/article/details/81983355

逻辑比较简单,直接上代码,可直接在小程序或node端引入使用。

//获取当前时间返回
const getCurrentMonthFirst = () => {
  let date = new Date();
  return date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "-" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
}
/**
 * 传入时间后几天
 * param:传入时间:dates:"2018-04-02",later:往后多少天
 */
const dateLater = (dates, later) => {
  let dateObj = {};
  let show_day = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  let date = new Date(dates);
  date.setDate(date.getDate() + later);
  let day = date.getDay();
  dateObj.year = date.getFullYear();
  dateObj.month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
  dateObj.day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  dateObj.week = show_day[day];
  return dateObj;
}

//获取d当前时间多少天后的日期和对应星期
const getDates = (days, todate = getCurrentMonthFirst()) => {
  let dateArry = [];
  for (var i = 0; i < days; i++) {
    var dateObj = dateLater(todate, i);
    dateArry.push(dateObj)
  }
  return dateArry;
}

//导出
module.exports = {
  getDates: getDates
}

引用:

const getDates = require('../../utils/day.js');

//获取往后十天的日期带星期
let res = getDates.getDates(10);
console.log(res)

猜你喜欢

转载自blog.csdn.net/Ruffaim/article/details/81983355
今日推荐