javascript 检查日期验证是否大于今天的日期以及 js 中的日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15031070/
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-26 23:13:18  来源:igfitidea点击:

check date validation with greater than today's date along with date format in js

javascriptvalidationdate

提问by Srim

How to validate the date in terms of 'dd-mm-yyyy H:i:s ' format and that date should be greater than system's date and time. This validation should be in javascript. Thanks in advance.

如何根据 'dd-mm-yyyy H:i:s ' 格式验证日期,并且该日期应大于系统的日期和时间。此验证应在 javascript 中。提前致谢。

回答by Niels

You can do this for example (live Fiddle: http://jsfiddle.net/7jrQZ/):

例如,您可以这样做(现场小提琴:http: //jsfiddle.net/7jrQZ/):

function parseDate(str)
{
    var s = str.split(" "),
        d = str[0].split("-"),
        t = str[1].replace(/:/g, "");
    return d[2] + d[1] + d[0] + t;
}

if( parseDate("17-05-1989 12:15:00") > parseDate("15-05-1989 14:00:00") )
{
    alert("larger")
}
else
{
   alert("smaller")   
}

回答by Sachin

Niels 's logic is right but the function he had written is incorrect.
parseDateis not returning correct value. you can check both values here. Niel's Fiddle update

Niels 的逻辑是对的,但是他写的函数是错误的。
parseDate没有返回正确的值。您可以在此处检查这两个值。尼尔的小提琴更新

Here is the correct function : correct fiddle

这是正确的功能:正确的小提琴

function parseDate(str)
{
    var s = str.split(" "),
        d = s[0].split("-"),
        t = s[1].replace(/:/g, "");
    return d[2] + d[1] + d[0] + t;
}

if( parseDate("17-05-1989 12:15:00") > parseDate("15-05-1989 14:00:00") )
{
    alert("larger");
}
else
{
   alert("smaller");  
}