0

I have a multi part problem. I am creating a website and for part of the website I want to include todays date. I know there is a simple way to get todays date using date(). but I don't like that method because it prints out a very technical date.

I am using javascript to create a function that will get todays date for me. I just want it to print out something like "Tuesday May 23, 2017".

<head>
       <title> My website </title>
 </head>

<body>
<h1 id="getTIME"> Read something here </h1>

<script>
function getFormattedDate(today)
{
var week = new Array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday);
var day = week[today.getDay()];
var calender = new Array('january', 'febuary', 'march', 'april', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
var month = calender[today.getMonth()+1];
var dd = today.getDay();
var year = today.getFullYear();

return day + calender +','+ dd + year;
}
var date=getFormattedDate(date);
document.getElementById("getTIME").innerHTML = date;
</script>
</body>

I know there is something wrong with my javascript But i think its very close to finding the date how I want it to.

The next part of my question is that i am putting the date in h1. but everything else that I write in h1 wont appear. Is there a way to make the text "Read something here" and the date show up on h1 at the same time?

  • 1
    If you were to use an actual `date` type, you could make use of [`toLocaleString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString), which has plenty of formatting options. If you click on that link and go to the "Examples" section, you'll see plenty of formats already laid out, along with instructions on how to create your own. – Tyler Roper May 23 '17 at 21:37
  • 1
    Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – MaxZoom May 23 '17 at 21:42

3 Answers3

1
  1. Date().toLocaleDateString() already does most of the work that your getFormattedDate function does.

  2. You are replacing the innerHTML of your H1 with the date (not appending to it). To append the date to your H1 try

    document.getElementById("getTIME").innerHTML += date;

James
  • 14,812
  • 2
  • 21
  • 36
0

You can concatenate your text to the existing content by using the += operator when you assign to the innerHTML of your h1.

function getFormattedDate(today) {
  var week = new Array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
  var day = week[today.getDay()];
  var calender = new Array('january', 'febuary', 'march', 'april', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
  var month = calender[today.getMonth()+1];
  var dd = today.getDay();
  var year = today.getFullYear();

  return day + ' ' + month +', '+ dd + ' ' + year;
}
var date=getFormattedDate(new Date());
document.getElementById("getTIME").innerHTML += date;
<h1 id="getTIME"> Read something here </h1>
Joseph Marikle
  • 68,672
  • 14
  • 103
  • 120
0

This will do it:

const months = ['january', 'febuary', 'march', 'april', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
const date = new Date()
const prettyDate = `${days[date.getDay()]} ${months[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()}`
console.log(prettyDate)