尽管JavaScript中的条件为true,但不会显示值

时间:2018-10-15 10:43:22

标签: javascript

下面是我的代码,其中显示了最新的B'day和周年纪念愿望(假设事件发生在同一天或15天)。 在下面的代码中,Jac's的生日是3rd of October,仍然显示其名称。

警告alert((datediff(parseDate(dates[1]), (today)) <= 15) && (datediff(parseDate(dates[1]), (today)) >= 1));显示值为true。静止名称不显示。

function parseDate(str) {
    var mdy = str.split('/');
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}
    // Take the difference between the dates and divide by milliseconds per day.
    // Round to nearest whole number to deal with DST.
function datediff(first, second) {
    return Math.round((second-first)/(1000*60*60*24));
}
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!

var y = today.getFullYear();
var dates = ["10/15/" + y, "10/3/" + y, "10/15/" + y, "9/6/" + y, "10/10/" + y, "10/1/" + y]; // MM/DD format
var names = ["Mac", "Jac", "Tom", "Abhay", "Mahesh", "Jayesh"];
var joingDates = ["10/16/" + y, "09/20/" + y, "10/2/" + y, "9/6/" + y, "10/10/" + y, "10/3/" + y];
var joiningyears = ["2000","2002","2010","2011","2011","2014"];

var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
//alert((datediff(parseDate(dates[1]), (today)) <= 15) && (datediff(parseDate(dates[1]), (today)) >= 1));

for(var i = 0; i < dates.length; i++) {
//document.getElementById("mySup").innerHTML = getGetOrdinal(dates[i].split('/')[0]);

  if ((datediff(parseDate(dates[i]), (today)) <= 15) && (datediff(parseDate(dates[i]), (today)) >= 1))
    {
 document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + 
                                             "<li>" +
                                             "Wishing a very" + " Happy Birthday to ".bold().fontcolor("blue") +
                                             names[i].bold().fontcolor("Red") + 
                                             "!!! (" + 
                                             getGetOrdinal(dates[i].split('/')[1]).split('')[0] + 
                                             getGetOrdinal(dates[i].split('/')[1]).split('')[1] + 
                                             getGetOrdinal(dates[i].split('/')[1]).split('')[2].sup() +
                                             getGetOrdinal(dates[i].split('/')[1]).split('')[3].sup() +                                             
                                             " " + 
                                             months[dates[0].split('/')[0]-1] +
                                             ")" + 
                                             "<br>";
    }
}

for(var i = 0; i < joingDates.length; i++) {
  if (datediff(parseDate(joingDates[i]), (today)) <= 15 && datediff(parseDate(joingDates[i]), (today)) >= 1)
    {
 document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML +
                                             "<li>" +
                                             "Wishing a very " +
                                             "Happy ".bold().fontcolor("blue") + 
                                             getGetOrdinal(y-joiningyears[i]).split('')[0].bold().fontcolor("blue") +
                                             getGetOrdinal(y-joiningyears[i]).split('')[1].sup().bold().fontcolor("blue") + 
                                             getGetOrdinal(y-joiningyears[i]).split('')[2].sup().bold().fontcolor("blue") + 
                                             " Work Anniversary to ".bold().fontcolor("blue") +
                                             names[i].bold().fontcolor("Red") + 
                                             " with " + 
                                             "Siemens".fontcolor("green").bold() + 
                                             "!!! (" + 
                                             getGetOrdinal(joingDates[i].split('/')[1]).split('')[0] + 
                                             getGetOrdinal(joingDates[i].split('/')[1]).split('')[1] + 
                                             getGetOrdinal(joingDates[i].split('/')[1]).split('')[2].sup() +
                                             getGetOrdinal(dates[i].split('/')[1]).split('')[3].sup() +
                                             " " + 
                                             months[joingDates[0].split('/')[0]-1] +
                                             ")" + 
                                             "<br>";
    }
}

function getGetOrdinal(n) {
    var s=["th","st","nd","rd"],
    v=n%100;
    return n+(s[(v-20)%10]||s[v]||s[0]);
 }
<p id="demo"><p>

1 个答案:

答案 0 :(得分:0)

通过将文本包含在"<sup>""</sup>"中,它可以正常工作。谢谢大家的建议。

function parseDate(str) {
    var mdy = str.split('/');
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}
    // Take the difference between the dates and divide by milliseconds per day.
    // Round to nearest whole number to deal with DST.
function datediff(first, second) {
    return Math.round((second-first)/(1000*60*60*24));
}
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!

var y = today.getFullYear();
var dates = ["10/15/" + y, "10/03/" + y, "10/15/" + y, "09/06/" + y, "10/10/" + y, "10/01/" + y]; // MM/DD format
var names = ["Mac", "Jac", "Tom", "Abhay", "Mahesh", "Jayesh"];
var joingDates = ["10/16/" + y, "09/20/" + y, "10/02/" + y, "09/06/" + y, "10/10/" + y, "10/04/" + y];
var joiningyears = ["2000","2002","2010","2011","2011","2014"];

var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];

for(var i = 0; i < dates.length; i++) {
//document.getElementById("mySup").innerHTML = getGetOrdinal(dates[i].split('/')[0]);

  if ((datediff(parseDate(dates[i]), (today)) <= 15) && (datediff(parseDate(dates[i]), (today)) >= 1))
    {
 document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + 
                                             "<li>" +
                                             "Wishing a very" + " Happy Birthday to ".bold().fontcolor("blue") +
                                             names[i].bold().fontcolor("Red") + 
                                             "!!! (" + 
                                             getGetOrdinal(dates[i].split('/')[1]).split('')[0] + 
                                             getGetOrdinal(dates[i].split('/')[1]).split('')[1] + 
                                             "<sup>" +
                                             getGetOrdinal(dates[i].split('/')[1]).split('')[2] +
                                             getGetOrdinal(dates[i].split('/')[1]).split('')[3] +                                             
                                             "</sup> " +
                                             " " + 
                                             months[dates[0].split('/')[0]-1] +
                                             ")" + 
                                             "<br>";
    }
}

for(var i = 0; i < joingDates.length; i++) {
  if (datediff(parseDate(joingDates[i]), (today)) <= 15 && datediff(parseDate(joingDates[i]), (today)) >= 1)
    {
 document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML +
                                             "<li>" +
                                             "Wishing a very " +
                                             "Happy ".bold().fontcolor("blue") + 
                                             getGetOrdinal(y-joiningyears[i]).split('')[0].bold().fontcolor("blue") +
                                             "<sup>" +
                                             getGetOrdinal(y-joiningyears[i]).split('')[1].bold().fontcolor("blue") +
                                             getGetOrdinal(y-joiningyears[i]).split('')[2].bold().fontcolor("blue") +
                                             "</sup> " + 
                                             " Work Anniversary to ".bold().fontcolor("blue") +
                                             names[i].bold().fontcolor("Red") + 
                                             " with " + 
                                             "Siemens".fontcolor("green").bold() + 
                                             "!!! (" + 
                                             getGetOrdinal(joingDates[i].split('/')[1]).split('')[0] +
                                             getGetOrdinal(joingDates[i].split('/')[1]).split('')[1] +
                                             "<sup>" +
                                             getGetOrdinal(joingDates[i].split('/')[1]).split('')[2] +
                                             getGetOrdinal(joingDates[i].split('/')[1]).split('')[3] + 
                                             "</sup> " + 
                                             months[joingDates[0].split('/')[0]-1] +
                                             ")" + 
                                             "<br>";
    }
}

function getGetOrdinal(n) {
    var s=["th","st","nd","rd"],
    v=n%100;
    return n+(s[(v-20)%10]||s[v]||s[0]);
 }
<p id="demo"><p>