0

I know my attempt it terrible, but this is what I got. I'm trying to get the current date to display in this fashion: Monday, 19 February 2018. Can anyone fix this mess into a working Javascript code?

var dayname = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November", "December");

var d=new Date();
var today = dayname[d.getDay()] + ", " + d.getDate() + " " + monthname[d.getMonth()] + " " + d.getFullYear();
document.getElementById('currentdate").innerHTML = today;
4
  • 2
    Using moment.js would make this much easier. Commented Feb 19, 2018 at 6:22
  • 4
    Possible duplicate of Where can I find documentation on formatting a date in JavaScript? Commented Feb 19, 2018 at 6:24
  • syntax error, replace this ('currentdate") with ("currentdate"). Commented Feb 19, 2018 at 6:26
  • careful... getMonths() starts at zero, getDays() starts at 1.... Commented Feb 19, 2018 at 14:02

2 Answers 2

1

You have syntax error here 'currentdate" either use all "" or '' otherwise your code is working just fine...

var dayname = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November", "December");

var d=new Date();
var today = dayname[d.getDay()] + ", " + d.getDate() + " " + monthname[d.getMonth()] + " " + d.getFullYear();
document.getElementById('currentdate').innerHTML = today;
// Put a ' instead of "                 ^ here
<div id="currentdate"></div>

The approach I would suggest you here is to still use moment.js

var date = moment().format("dddd, Do MMMM YYYY");
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

@brk yea I was editing my answer to give him the moment.js approach.. :)
0

var dayname = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November", "December");

var d=new Date();
var today = dayname[d.getDay()] + ", " + d.getDate() + " " + monthname[d.getMonth()] + " " + d.getFullYear();
document.getElementById("currentdate").innerHTML = today;
<span id="currentdate"></span>

document.getElementById('currentdate") this is the problem. you have used one ' and "

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.