首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的代码不正确地比较日期?

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

Stack Overflow用户
提问于 2021-11-29 10:22:34
回答 2查看 56关注 0票数 0

我想检查一下日期是否在这个范围内。我创建了两个函数,第一个func转换类型str至今,第二个func必须找到结果。

代码语言:javascript
复制
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);

但在开始后,所有日期的结果都是‘外侧范围!’。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-29 10:33:13

与代码有关

  • 函数中的toDate错误。授权应该是return new Date(year, +month - 1, day);。不需要添加1和日期。另外,对于年份和日期来说,数字也不是必须的,它们也可以是字符串。get_staticstic.

minDatemaxDate

  • 错误

工作部件

代码语言:javascript
复制
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)方法直接将日期对象转换为日期对象。

工作部件

代码语言:javascript
复制
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);

票数 2
EN

Stack Overflow用户

发布于 2021-11-29 10:28:31

您应该将minDate设置为开始日期,maxDate设置为结束日期。你做了相反的事。

代码语言:javascript
复制
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);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70153026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档