Get full date in JS -Error display number day

Hi coders,
Today I start to create a todolist and I’m searching some inspirations.
Anyway I want to display date like this in Javascript language:

" Monday 25th November "

For now I have implemented this by looking at some resources:

// View current date 
var date = 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 dayTodayName = weekday[date.getDay()];
var dayToday =date.toLocaleString('default',{dayToday:'numeric'});
var month = date.toLocaleString('default',{month:'long'});
document.getElementById("date").innerHTML=dayTodayName + " "+ dateToday + " " + month;

The result is this:

Monday Mon Nov 25 2019 15:54:55 GMT+0100 (Central European Standard Time) November

I don’t know how to show only the number within my date.
Thanks for help!!!

You could do something like:

const convertDayNum = num => {
  const specialDayEndings = {
    1: 'st', 2: 'nd', 3: 'rd',
    21: 'st', 22: 'nd', 23: 'rd',
    31: 'st'
  return num + (specialDayEndings[num] ? specialDayEndings[num] : 'th');
const today = new Date();
const dayName =  today.toLocaleDateString('default', { weekday: 'long' });
const dayNum = today.toLocaleDateString('default', { day: 'numeric' });
const monthName = today.toLocaleDateString('default', { month: 'long' });
document.getElementById("date").innerHTML= `${dayName} ${convertDayNum(dayNum)} ${monthName}`;

There are several ways to implement the convertDayNum function but the one above was quick and dirty.

1 Like

You could import and use momentjs, when it comes to dealing with dates, it has pretty much everything you would want revolving around dates, as long this is a project outside of the curriculum.

1 Like