JavaScript获取往后天数带星期

逻辑比较简单,直接上代码,可直接在小程序或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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值