我想检查一下日期是否在这个范围内。我创建了两个函数,第一个func转换类型str至今,第二个func必须找到结果。
let probeArray = [{price: 123, date: '2021-11-27'},
{price: 13, date: '2021-11-15'},
{price: 1, date: '2021-10-2'},
{price: 17, date: '2021-10-1'}];
let startDate = '2021-10-1';
let endDate = '2021-10-20';
// transform str to Date
const toDate = (dateStr) => {
const [year,month,day] = dateStr.split("-");
// console.log('check date')
// console.log([day, month, year])
return new Date(year, month - 1, +day+1);
}
function get_staticstic(probeAr, start, end){
let result = null;
let maxDate = toDate(start);
let minDate = toDate(end);
for (let tempEvent of probeAr){
let currentDate = toDate(tempEvent.date);
console.log('maxDate', maxDate);
console.log('minDate', minDate);
console.log('currentDate',currentDate);
if (currentDate >= minDate && currentDate <= maxDate ){
console.log('Correct Date');
}
else{
console.log('Out Side range!!');
}
}
return result
}
get_staticstic(probeArray, startDate, endDate);但在开始后,所有日期的结果都是‘外侧范围!’。

发布于 2021-11-29 10:33:13
与代码有关
toDate错误。授权应该是return new Date(year, +month - 1, day);。不需要添加1和日期。另外,对于年份和日期来说,数字也不是必须的,它们也可以是字符串。get_staticstic.中minDate和maxDate的
工作部件
let probeArray = [
{ price: 123, date: '2021-11-27' },
{ price: 13, date: '2021-11-15' },
{ price: 1, date: '2021-10-2' },
{ price: 17, date: '2021-10-1' }
];
let startDate = '2021-10-1';
let endDate = '2021-10-20';
// transform str to Date
const toDate = (dateStr) => {
const [year, month, day] = dateStr.split("-");
// console.log('check date')
// console.log([day, month, year])
return new Date(year, +month - 1, day);
}
function get_staticstic(probeAr, start, end) {
let result = null;
let minDate = toDate(start);
let maxDate = toDate(end);
console.log('maxDate', maxDate);
console.log('minDate', minDate);
for (let tempEvent of probeAr) {
let currentDate = toDate(tempEvent.date);
console.log('currentDate', currentDate);
if (currentDate >= minDate && currentDate <= maxDate) {
console.log('Correct Date');
}
else {
console.log('Out Side range!!');
}
}
return result
}
get_staticstic(probeArray, startDate, endDate);
更好的方法
因为所有的日期字符串都是标准格式的,所以不需要为日期编写解析器函数。可以使用new Date(dateString)方法直接将日期对象转换为日期对象。
工作部件
let probeArray = [
{ price: 123, date: '2021-11-27' },
{ price: 13, date: '2021-11-15' },
{ price: 1, date: '2021-10-2' },
{ price: 17, date: '2021-10-1' }
];
let startDate = '2021-10-1';
let endDate = '2021-10-20';
function get_staticstic(probeAr, start, end) {
let result = null;
let minDate = new Date(start);
let maxDate = new Date(end);
console.log('maxDate', maxDate);
console.log('minDate', minDate);
for (let tempEvent of probeAr) {
let currentDate = new Date(tempEvent.date);
console.log('currentDate', currentDate);
if (currentDate >= minDate && currentDate <= maxDate) {
console.log('Correct Date');
}
else {
console.log('Out Side range!!');
}
}
return result
}
get_staticstic(probeArray, startDate, endDate);
发布于 2021-11-29 10:28:31
您应该将minDate设置为开始日期,maxDate设置为结束日期。你做了相反的事。
let probeArray = [{price: 123, date: '2021-11-27'},
{price: 13, date: '2021-11-15'},
{price: 1, date: '2021-10-2'},
{price: 17, date: '2021-10-1'}];
let startDate = '2021-10-1';
let endDate = '2021-10-20';
// transform str to Date
const toDate = (dateStr) => {
const [year,month,day] = dateStr.split("-");
// console.log('check date')
// console.log([day, month, year])
return new Date(year, month - 1, +day+1);
}
function get_statistic(probeAr, start, end){
let result = null;
let minDate = toDate(start);
let maxDate = toDate(end);
for (let tempEvent of probeAr){
let currentDate = toDate(tempEvent.date);
console.log('maxDate', maxDate);
console.log('minDate', minDate);
console.log('currentDate',currentDate);
if (currentDate >= minDate && currentDate <= maxDate ){
console.log('Correct Date');
}
else{
console.log('Out Side range!!');
}
}
return result
}
get_statistic(probeArray, startDate, endDate);
https://stackoverflow.com/questions/70153026
复制相似问题