我尝试了这个方法来检查带有日期字符串数组的数组中的重复日期,但没有成功。请任何人帮忙。。
const dateArray = ["2000-07-13","03/24/2000", "June 7 2021"]
const compaingDate =new Date("2000-06-13")
let countOfDays=0
for(let sameDate of dateArray){
if(compaingDate.getDate()===sameDate.getDate()){
countOfDays+=1
}
}
console.log(countOfDays);
从不将字符串数组转换为日期数组:
const dateArray = ["2000-07-13","03/24/2000", "June 7 2021", "June 13 2006"]
const compaingDate =new Date("2000-06-13")
let countOfDays=0
for(let sameDate of dateArray){
sameDate = new Date(sameDate)
if(compaingDate.getDate()===sameDate.getDate()){
countOfDays+=1
}
}
console.log(countOfDays);
也许这就是您想要的?
缩短版:
const dateArray = ["2000-07-13", "03/24/2000", "June 7 2021"]
const compaingDate = new Date("2000-06-13")
let countOfDays = 0
dateArray.forEach(item => {
if (new Date(item).getDate() === compaingDate.getDate())
countOfDays++
})
console.log(countOfDays);