1

With below code I'm trying to write the code to display recent (from date of b'day to 15 days) b'day wishes. At present nothing is displayed. I'm new to JavaScript, so need your help.

var dates = ["02/09/2009", "12/10/2010", "02/01/2001"];
var names = ["Mac", "Jac", "Tom"];
var today = new Date();

alert(today.getMonth()+1);
//alert(today.getDay());

//alert(typeof(day));

for(var i = 0; i < dates.length; i++) {
  if (dates[i].split('/')[0] = today.getDay() && dates[i].split('/')[1] = today.getMonth()+1 && dates[i].split('/')[0] <= today.getDay()+15)
    {
 document.getElementById("demo").innerHTML = "Wish You Happy Birthday, " + names[dates.indexOf(dates[i])] + "(" + dates[i] + ")";
    }
    else{
    document.getElementById("demo").innerHTML = "No matches";
    }
}
<p id="demo"></p>
Experimenter
  • 167
  • 1
  • 14
  • `d.setMonth(s[1])` ... you seem to have forgotten your own comment from the top of your code `//January is 0!` - so perhaps ... `d.setMonth(s[1]-1);` - personally though, I'd set the date to 1, then set the month, then set the date to the right date ... because not all months have the same length ... and if today was `31/07` and the DOB was in `30/06`, you'd end up with fDate returning `30/07` – Jaromanda X Oct 02 '18 at 10:28
  • but ... see how your executable snippet throws an error `"message": "TypeError: document.getElementById(...) is null` – Jaromanda X Oct 02 '18 at 10:35
  • @JaromandaX Yes, you're right. But I couldn't resolve that error. – Experimenter Oct 02 '18 at 10:39
  • `document.getElementById("DATE").value = today;` ... you have no such element – Jaromanda X Oct 02 '18 at 10:39
  • yes, this has nothing to do with main code, I'll remove it. – Experimenter Oct 02 '18 at 10:43
  • at least then you'll get to see the issue when you click the button ... i.e. whata I pointed out in the very first comment - also, `today` is a string representation of a date, where as `fDate` returns a date ... so you can't compare those – Jaromanda X Oct 02 '18 at 10:44
  • Any other way to accomplish this... – Experimenter Oct 02 '18 at 15:44

2 Answers2

0

At last, with 3rd approach I could make it work, that you all for your help!

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/16/" + y, "09/20/" + y, "10/2/" + y, "9/6/" + y, "10/10/" + y, "10/3/" + y];
var names = ["Mac", "Jac", "Tom", "Abhay", "Mahesh", "Jayesh"];
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)) <= 7 && datediff(parseDate(dates[i]), (today)) >= 1)
    {
 document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "Wish You Happy Birthday, " + names[dates.indexOf(dates[i])] + "!!! (" + getGetOrdinal(dates[i].split('/')[1]) + " " + months[dates[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><span id="demo"></span><sup id="mySup"></sup><p>
Experimenter
  • 167
  • 1
  • 14
0

The below answer is what I had to do finally which involves functions, methods, etc. I'm sure it might be helpful to someone.

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/1/" + y, "09/20/" + y, "10/2/" + y, "9/6/" + y, "10/10/" + y, "10/1/" + y];
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"];

for(var i = 0; i < dates.length; i++) {
//document.getElementById("mySup").innerHTML = getGetOrdinal(dates[i].split('/')[0]);
  if (datediff(parseDate(dates[i]), (today)) <= 7 && 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].sup() + 
                                             " " + 
                                             months[dates[0].split('/')[0]-1] +
                                             ")" + 
                                             "<br>";
    }
}

for(var i = 0; i < joingDates.length; i++) {
  if (datediff(parseDate(joingDates[i]), (today)) <= 7 && 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 " + 
                                             "XYZ Company".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].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>
Experimenter
  • 167
  • 1
  • 14