js获取两个日期之间的日期

方法一:使用循环遍历日期范围并逐个添加到一个数组中

function getDatesBetween(startDate, endDate) {
  const dates = [];
  const current = new Date(startDate);
  
  while(current <= endDate) {
    dates.push(new Date(current));
    current.setDate(current.getDate() + 1);
  }
  
  return dates;
}
 
// 使用示例
const startDate = new Date('2022-12-21');
const endDate = new Date('2023-01-10');
const dates = getDatesBetween(startDate, endDate);
console.log(dates);

方法二:使用moment.js库来处理日期计算

const moment = require('moment');
 
function getDatesBetween(startDate, endDate) {
  const dates = [];
  const current = moment(startDate);
  
  while(current <= endDate) {
    dates.push(current.clone().toDate());
    current.add(1, 'day');
  }
  
  return dates;
}
 
const startDate = moment('2023-01-11');
const endDate = moment('2023-02-10');
const dates = getDatesBetween(startDate, endDate);
console.log(dates);

YYYY-MM-DD 二个日期间的日期,相同年月分组

function getDayAll(starDay, endDay) {
     var arr = [];
     var dates = [];
     // 设置两个日期UTC时间
     var db = new Date(starDay);
     var de = new Date(endDay);
     // 获取两个日期GTM时间
     var s = db.getTime() - 24 * 60 * 60 * 1000;
     var d = de.getTime() - 24 * 60 * 60 * 1000;
     
     // 获取到两个日期之间的每一天的毫秒数
     for (var i = s; i <= d;) {
       i = i + 24 * 60 * 60 * 1000;
       arr.push(parseInt(i))
     }
     // 获取每一天的时间  YY-MM-DD
     for( var j in arr ){
       var time = new Date(arr[j]);
       var year = time.getFullYear(time);
       var mouth = (time.getMonth() + 1)>=10?(time.getMonth() + 1):('0'+(time.getMonth() + 1));
       var day = time.getDate()>=10?time.getDate():('0'+time.getDate());
       var YYMMDD = year + '-' + mouth + '-' + day;
       dates.push(YYMMDD)
     }
     return dates
   },


  let timeArr1 = this.getDayAll('2023-06-01', '2023-07-05');
  // 相同年月分组
   let newobj = {}
   timeArr1.forEach(row=>{
     let key = row.slice(0,7)
     newobj[key] = newobj[key] || []
     newobj[key].push(row)
   })

获取某年某月有多少天

function getDaysInMonth(year, month) {
  return new Date(year, month, 0).getDate();
} 
console.log(getDaysInMonth(2024,01)) // 31
console.log(getDaysInMonth(2024,1)) // 31

判断二个日期在当前日期是处于什么状态(未执行、执行中、完成)

 // 当前日期有没有在开始时期和结束日期之间,true的话就是执行中
  isExecuting (start,end){
     let currentDate = new Date();
     let startDate = new Date(start);
     let endDate = new Date(end);
     let istrue = currentDate >= startDate && currentDate <= endDate;
     return istrue; 
   },
   // 判断当前日期是否小于开始日期,true的话就是未执行
   isUnexecuted(start){
     let currentDate = new Date();
     let startDate = new Date(start);
     let istrue = currentDate < startDate;
     return istrue;
   }, 
    // 判断当前日期是否大于结束日期,true的话就是完成
    isFinished(end){
     let currentDate = new Date();
     let endDate = new Date(end);
     let istrue = currentDate > endDate;
     return istrue;
   },
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值