check whether the date entered by the user is current date or the future date

前端 未结 5 547
离开以前
离开以前 2020-12-18 19:50

I was browsing through the net to find a javascript function which can check whether the date entered by the user is current date or the future date but i didn\'t found a s

5条回答
  •  不知归路
    2020-12-18 20:16

    You can compare two dates as if they were Integers:

    var now = new Date();
    if (before < now) {
      // selected date is in the past
    }
    

    Just both of them must be Date.

    First search in google leads to this: Check if date is in the past Javascript

    However, if you love programming, here's a tip:

    1. A date formatted like YYYY-MM-DD could be something like 28-12-2013.
    2. And if we reverse the date, it is 2013-12-28.
    3. We remove the colons, and we get 20131228.
    4. We set an other date: 2013-11-27 which finally is 20131127.
    5. We can perform a simple operation: 20131228 - 20131127

    Enjoy.

提交回复
热议问题