1

I'm trying to display the full date. How do I modify this script to display the FULL day not just the abbreviation.

e.g. Monday October 26, 2015 NOT Mon Oct 26 2015

var d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();
<p id="demo"></p>
j08691
  • 204,283
  • 31
  • 260
  • 272
Jonnie
  • 71
  • 9
  • 1
    The Date *toString* method is [*entirely implementation dependant*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-date.prototype.tostring). If you want a particular format, write your own function (pretty simple) or use a library (overkill if this is all you want to do). There are many, many questions on SO about [*formatting date strings*](http://stackoverflow.com/search?q=%5Bjavascript%5D+format+date+string). – RobG Oct 27 '15 at 00:39
  • You should post your actual javascript code, not just the output. – Mike Oct 27 '15 at 00:41

2 Answers2

1

This will help you:

var a = new Date();
var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
var dayNames = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];

var date = dayNames[a.getDay()-1] + " " + monthNames[a.getMonth()] + " " + a.getDate() + " " + a.getFullYear();

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

Use a similar string for the names of day of the week

  • Actually, `getDay()` is different from `getMonth` in how it returns. – Max Oct 27 '15 at 01:00
  • Yep I forgot about this – Mauricio Gómez Oct 27 '15 at 01:58
  • Cheers!! Its amazes me every time I use Stack Overflow how fast the community responds. Thank you so much! – Jonnie Oct 27 '15 at 03:22
  • ended up using: var d = new Date(); var weekday = new Array(7); weekday[0] = "Sunday"; weekday[1] = " Monday "; weekday[2] = "Tuesday"; weekday[3] = "Wednesday"; weekday[4] = "Thursday"; weekday[5] = "Friday"; weekday[6] = "Saturday"; var n = weekday[d.getDay()]; var a = new Date(); var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", " October ", "November", "December"]; var date = weekday[d.getDay()] + monthNames[a.getMonth()] + " " + a.getDate() + ", " + a.getFullYear(); document.getElementById("date").innerHTML = date; – Jonnie Oct 27 '15 at 03:25
0

You could try something of this manner. date.getDay() will return a numerical representation of the week day. Depending on the day or month you draw a different element from the array:

var date = new Date();
var day = date.getDay();
var month = date.getMonth();
var monthNames = ["January", "February", "March","April", "May","June", "July","August", "September", "October","November", "December"];
var dayNames = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];

alert(monthNames[month]);
alert(dayNames[day-1]);

Demo Code:

http://jsfiddle.net/7e578hm8/2/

Max
  • 2,710
  • 1
  • 23
  • 34