0

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>
2
  • can you post your HTML as well? Commented Jun 12, 2016 at 20:57
  • @ALEX, more elegant solutions could be found here stackoverflow.com/questions/15397372/… Commented Jun 12, 2016 at 21:46

2 Answers 2

1

You have some issues with your code. Firstly how are you define 'today'?. Secondly, the slice function is not defined on Number object. With these two edits your working 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 today = new Date();

   var date = today.getDate();

   date = checkDate(String(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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to define today before using it, as follows:

var today = new Date();
var date = today.getDate();

The function checkDate receives a numeric argument, but then treats it as a string by using slice: this produces an error. You should treat it as a number, for instance with i % 10:

function checkDate(i) {
   return (i % 10 == 1 && i !== 11) ? i + "st"
        : (i % 10 == 2 && i !== 12) ? i + "nd"
        : (i % 10 == 3 && i !== 13) ? i + "rd"
        : i + "th";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.