dimanche 12 juin 2016

Change variable's value if the same variable is equal to a string

I am working on a small project where I have to display the current date. I need to add nth, st, and so on depending on the last letter of the variable's string, but when I check the HTML file in a web browser, the string doesn't show up. Here is my code:

window.onload = function() {
   startDate();
}

function startDate() {
   var month = [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"
   ];

   var weekDay = [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"
   ];

   var date = today.getDate();

   date = checkDate(date);

   document.getElementById("date").innerHTML = weekDay[today.getDay()] + " the " + date + " of " + month[today.getMonth()];
}

function checkDate(i) {
   if (i.slice(-1) == 1 && i !== 11) {
      i = i + "st";
   } else if (i.slice(-1) == 2 && i !== 12) {
      i = i + "nd";
   } else if (i.slice(-1) == 3 && i !== 13) {
      i = i + "rd";
   } else {
      i = i + "th";
   }

   return i;
}

Here's my HTML:

<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8">
   <link rel="stylesheet" href="style.css" />
</head>
<body>
   <div id="container">
      <div id="date"></div>
   </div>
   <script src="script.js"></script>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire