简体   繁体   English

为什么我的代码不能正确比较日期?

[英]Why my code don't compare correctly dates?

I would like to check does the date fall within the range.我想检查日期是否在范围内。 I create two functions, first func transforms type str to Date,second func must find result.我创建了两个函数,第一个函数将 str 类型转换为日期,第二个函数必须找到结果。

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);

But after start result for all dates is 'Out Side range.!'.但是在所有日期的开始结果之后都是'Out Side range.!'。

out_result

Issue with code代码问题

  • Error in toDate function. toDate function 中的错误。 Defition should be return new Date(year, +month - 1, day);定义应该是return new Date(year, +month - 1, day); . . No need to add 1 with date.无需添加 1 和日期。 Also its not mandatory for the year and day to be number, they can be string aswell.年份和日期也不是强制性的,它们也可以是字符串。
  • Error with minDate and maxDate inside get_staticstic . get_staticstic 中的minDatemaxDate get_staticstic

Working Fiddle工作小提琴

 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);

Better Approach更好的方法

Since all date strings ae in standard format, you dont need to write a parser function for date.由于所有日期字符串都是标准格式的,因此您不需要为日期编写解析器 function。 You can directly convert to date object using new Date(dateString) method.您可以使用new Date(dateString)方法直接转换为日期 object。

Working Fiddle工作小提琴

 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);

You should set minDate to the start date and maxDate to the end date.您应该将minDate设置为开始日期, maxDate设置为结束日期。 You did the opposite.你反其道而行之。

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM