无法使用Javascript将给定日期与今天日期进行比较

时间:2018-01-17 06:22:48

标签: javascript

我面临一个问题。我需要使用Javascript将给定日期与今天日期进行比较。我在下面解释我的代码。

function getCurrentDate(){
    var today = new Date();
    var dd = today.getDate();

    var mm = today.getMonth()+1; 
    var yyyy = today.getFullYear();
    if(dd<10) 
    {
        dd='0'+dd;
    } 

    if(mm<10) 
    {
        mm='0'+mm;
    } 
    today = dd+'-'+mm+'-'+yyyy;
    return today;
}

var givendate=new Date('19-01-2018');
var todaydate=$scope.getCurrentDate();
if (givendate >= todaydate) {
    console.log('bool',true);
}else{
    console.log('bool',false);
}

这里我应该得到结果true但是在这里我得到的控制台消息为false。请帮我解决这个问题。

4 个答案:

答案 0 :(得分:1)

正如您在评论givendate "Invalid Date"中所说的那样 - 这就是您错误的原因。

因此,在解析日期之前,请检查how to detect invalid date

new Date尝试解析ISO 8601 format。引用:

  

格式如下。确切地说,此处显示的组件必须存在,   正是这个标点符号。注意&#34; T&#34;出现   字面意思是在字符串中,表示时间的开始   元素,如ISO 8601中所述。

   Year:
      YYYY (eg 1997)
   Year and month:
      YYYY-MM (eg 1997-07)
   Complete date:
      YYYY-MM-DD (eg 1997-07-16)
   Complete date plus hours and minutes:
      YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
   Complete date plus hours, minutes and seconds:
      YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
   Complete date plus hours, minutes, seconds and a decimal fraction of a
second
      YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:

     YYYY = four-digit year
     MM   = two-digit month (01=January, etc.)
     DD   = two-digit day of month (01 through 31)
     hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
     mm   = two digits of minute (00 through 59)
     ss   = two digits of second (00 through 59)
     s    = one or more digits representing a decimal fraction of a second
     TZD  = time zone designator (Z or +hh:mm or -hh:mm)

确保在构造日期对象时传递格式正确的日期。有关该主题的所有详细信息均可在JS MDN的Date.parse topic

中找到

答案 1 :(得分:1)

以下代码有效。与其他人说的一样,您需要确保以Date构造函数的预期正确格式指定日期。

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>
<p id="today"></p>
<p id="text"></p>

<script>
today = new Date();
jan19 = new Date('2018-01-19')
document.getElementById("demo").innerHTML = jan19;
document.getElementById("today").innerHTML = today;
if (today < jan19) {
    document.getElementById("text").innerHTML = 'true';
}
else {
    document.getElementById("text").innerHTML = 'false';
}
</script>

</body>
</html>

答案 2 :(得分:1)

如果您想继续使用当前日期格式( dd-MM-yyyy ),则可能需要将字符串拆分为-并相应重新排列以生成有效日期

JS代码

&#13;
&#13;
function getCurrentDate() {
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth() + 1;
        var yyyy = today.getFullYear();
        if (dd < 10)
            dd = '0' + dd;
            if (mm < 10)
                mm = '0' + mm;
            today = dd + '-' + mm + '-' + yyyy;
            return today;
        }

    var todayDate = getCurrentDate().toString();
    var givenDate = '19-01-2018';

    var todayDateArray = todayDate.split('-');
    var givenDateArray = givenDate.split('-');
    
    todayDate = new Date(todayDateArray[2] + '-' 
                       + todayDateArray[1] + '-' 
                       + todayDateArray[0]);
    
    givenDate = new Date(givenDateArray[2] + '-' 
                       + givenDateArray[1] + '-' 
                       + givenDateArray[0]);
    
    if(givenDate >= todayDate)
        console.log('bool', true);
    else
        console.log('bool', false);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

这对我有用。如果您不想要自定义格式化日期,则可以使用此日期。

&#13;
&#13;
function getCurrentDate(){
    var today = new Date();
    return today;
}

var givendate=new Date("2018-01-19");
var todaydate= getCurrentDate();
console.log(givendate);
console.log(todaydate);
if (givendate >= todaydate) {
    console.log('bool',true);
}else{
    console.log('bool',false);
}
&#13;
&#13;
&#13;

相关问题