1

So I'm trying to get my date displayed in my innerhtml but problem is I cant find out as to why its not spacing out or displaying like this Date image

My code JS code is

   function myFunction() {

  // day of the month, day of the week month and year
  var p = document.getElementById("mydata");
  var mydate = new Date();
  var day = mydate.getDay();
  var month = mydate.getMonth();
  var year = mydate.getYear();
  var d = mydate.getDate();
  var dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

  p.innerHTML = "Today is the " + d + dayNames[day] + month + year;


}

and html is

   <!DOCTYPE html>
<html lang="en">
<head>
  <!-- link to external JS file.  Note that <script> has an
  end </script> tag -->
  <meta charset="utf-8">
  <title> Task 5 </title>
  <link href="style.css" type="text/css" rel="stylesheet">
  <script src="task5.js" type="text/javascript"></script>
</head>
<body>
  <!-- Create a paragraph with id mydata -->
  <div id="box">
  <p id="mydata"> Todays Date</p>
  <p> <button  onclick="myFunction();"> Click </button></p>
</div>
</body>
</html>
3
  • It isn't clear how you want it to be displayed. Commented Oct 22, 2018 at 17:33
  • You need to add spaces to the string concatenation: "Today is the " + d + " (" + daynames[day] + ") " etc etc Commented Oct 22, 2018 at 17:36
  • 2
    "Today is the " + d + dayNames[day] + month + year; <--- look at what you are doing.... I see no spaces Commented Oct 22, 2018 at 17:42

4 Answers 4

3

you should add number to month and year, like this:

function myFunction() {

    var p = document.getElementById("mydata");
    var mydate = new Date();
    var day = mydate.getDay();
    var month = mydate.getMonth() + 1;
    var year = mydate.getYear() + 1900;
    var d = mydate.getDate();
    var dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

    p.innerHTML = "Today is " + d + "(" + dayNames[day] + ")" + month + " " + year;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your code has two issues.

  1. The year is relative to 1900, so you need to add that in.

    var year = mydate.getYear() + 1900;

  2. You need to explicitly add the spaces and the parentheses.

    p.innerHTML = "Today is " + d + " (" + dayNames[day] + ") " + month + " " + year;

Comments

1

Solved it, thanks guys.

function myFunction() {

  // day of the month, day of the week month and year
  var p = document.getElementById("mydata");
  var mydate = new Date();
  var day = mydate.getDay();
  var month = mydate.getMonth();
  var year = mydate.getYear() + 1900;
  var d = mydate.getDate();
  var dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  var months = ["Jan", "Feb", "Mar", "April", "May", "June", "July", "August", "September", "Oct"];

  p.innerHTML = "Today is the " + d + " " + "("+ dayNames[day]+")" + " " + months[month] + " " + year;
  " (" + months[month] + ")"


}

Comments

1

Use getFullYear for the year, get the correct month names and correctly build the date string.

function myFunction() {

  // day of the month, day of the week month and year
  var p = document.getElementById("mydata");
  var mydate = new Date();
  var day = mydate.getDay();
  var month = mydate.getMonth();
  var year = mydate.getFullYear();
  var d = mydate.getDate();
  var dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var monthShortNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

  p.innerHTML = "Today is the " + d + " (" + dayNames[day] + ") " + monthShortNames[month] + " " + year;
  
}
<!-- Create a paragraph with id mydata -->
<div id="box">
  <p id="mydata"> Todays Date</p>
  <p> <button onclick="myFunction();"> Click </button></p>
</div>

2 Comments

Also needs an array of month names just like dayNames
Added month names. Thanks.

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.