Comparing date part only without comparing time in JavaScript

后端 未结 22 1863
春和景丽
春和景丽 2020-11-22 10:59

What is wrong with the code below?

Maybe it would be simpler to just compare date and not time. I am not sure how to do this either, and I searched, but I couldn\'t

22条回答
  •  时光说笑
    2020-11-22 11:56

    This works for me:

     export default (chosenDate) => {
      const now = new Date();
      const today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
      const splitChosenDate = chosenDate.split('/');
    
      today.setHours(0, 0, 0, 0);
      const fromDate = today.getTime();
      const toDate = new Date(splitChosenDate[2], splitChosenDate[1] - 1, splitChosenDate[0]).getTime();
    
      return toDate < fromDate;
    };
    

    In accepted answer, there is timezone issue and in the other time is not 00:00:00

提交回复
热议问题