javascript 检查用户输入的日期是当前日期还是未来日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18712899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:57:39  来源:igfitidea点击:

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

javascript

提问by Ryan decosta

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 suitable answer so i made it myself.Wondering If this can be achieved by one line code.

我正在网上浏览以找到一个 javascript 函数,它可以检查用户输入的日期是当前日期还是未来日期,但我没有找到合适的答案,所以我自己做了。想知道这是否可以通过一行代码。

 function isfutureDate(value) 
    {    
    var now = new Date;
    var target = new Date(value);

    if (target.getFullYear() > now.getFullYear()) 
    {
        return true;
    }
    else if(target.getFullYear() == now.getFullYear()) 
    {
    if (target.getMonth() > now.getMonth()) {
    return true;
    } 
    else if(target.getMonth() == now.getMonth())
    {
    if (target.getDate() >= now.getDate()) {
        return true;
    }
    else
    {
        return false
    }
    }


    }

   else{
    return false;
    }
}   

回答by Reinherd

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.

只是他们两个都必须是Date。

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

在 google 中的第一次搜索导致:检查日期是否在过去 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
  1. 格式为 YYYY-MM-DD 的日期可能类似于 28-12-2013。
  2. 如果我们颠倒日期,那就是 2013-12-28。
  3. 我们去掉冒号,我们得到 20131228。
  4. 我们设置了另一个日期:2013-11-27,最终是 20131127。
  5. 我们可以执行一个简单的操作:20131228 - 20131127

Enjoy.

享受。

回答by denov

here's a version that only compares the date and excludes the time.

这是一个仅比较日期而排除时间的版本。

Typescript

打字稿

const inFuture = (date: Date) => {
    return date.setHours(0,0,0,0) > new Date().setHours(0,0,0,0)
};

ES6

ES6

const inFuture = (date) => {
    return date.setHours(0,0,0,0) > new Date().setHours(0,0,0,0)
};

回答by Prajakta Kale

try this

试试这个

function IsFutureDate(dateVal) {
    var Currentdate = new Date();
        dateVal= dateVal.split("/");
    var year = Currentdate.getFullYear();
    if (year < dateVal[2]) {
        return false;//future date

    }        
    else {
        return true; //past date
    }

}

回答by Khalid Habib

try out this

试试这个

function isFutureDate(idate){
var today = new Date().getTime(),
    idate = idate.split("/");

idate = new Date(idate[2], idate[1] - 1, idate[0]).getTime();
return (today - idate) < 0 ? true : false;
}

Demo

演示

console.log(isFutureDate("02/03/2016")); // true
console.log(isFutureDate("01/01/2016")); // false

回答by Seth

ES6 version with tolerable future option.

ES6 版本,具有可容忍的未来选项。

I made this little function that allows for some wiggle room (incase data coming in is from a slightly fast clock for example).

我做了这个小功能,允许一些摆动空间(例如,传入的数据来自一个稍微快一点的时钟)。

It takes a Dateobject and toleranceMilliswhich is the number of seconds into the future that is acceptable (defaults to 0).

它需要一个Date对象,toleranceMillis它是可接受的未来秒数(默认为 0)。

const isDistantFuture = (date, toleranceMillis = 0) => {
    // number of milliseconds tolerance (i.e. 60000 == one minute)
    return date.getTime() > Date.now() + toleranceMillis
}