Misha Moroshko
Misha Moroshko

Reputation: 171459

How to get the day of the week from the day number in JavaScript?

Given dayNumber is from 0 - 6 representing Monday - Sunday respectively.

Can the Date / String objects be used to get the day of the week from dayNumber?

Upvotes: 61

Views: 166291

Answers (15)

Jonathan
Jonathan

Reputation: 1449

This one-liner will return what you want in your browser's langauge e.g. English:

new Intl.DateTimeFormat(navigator.language,{weekday:'long'}).format((4+dayNumber)*864e5)

Change navigator.language to 'en','es','pt','fr',etc to get the weekday in a different language.

Change 'long' to 'short' to get "Mon" vs "Monday".

Notes: The code uses an offset by 4 since dates start on a Thursday (i.e. new Date(0) is Thursday 1 Jan 1970) and 864e5 in the number of milliseconds in a day.

Upvotes: 0

Lucas Pinheiro
Lucas Pinheiro

Reputation: 1

function whatday(num) { 
  switch(num) {
    case 1:
      return "Sunday";
    case 2:
      return "Monday";
    case 3:
      return "Tuesday";
    case 4:
      return "Wednesday";
    case 5:
      return "Thursday";
    case 6:
      return "Friday";
    case 7:
      return "Saturday";
    default:
      return 'Wrong, please enter a number between 1 and 7';
  }
}

Upvotes: -1

Watchmaker
Watchmaker

Reputation: 5308

Just to add my two cents... moment (which maybe is not the best library option) has a method:

moment.weekdays(1) // Monday
moment.weekdaysShort(1) //Mon

By the way it is locale dependent so it gives you the name in the language set, that is why I chose this option.

Upvotes: 1

Ali Raza
Ali Raza

Reputation: 1073

const language = 'en-us';
const options = {  weekday: 'long' };
const today = new Date().toLocaleString(language, options)
cosnsole.log(today)

Upvotes: 4

Kevin Oswaldo
Kevin Oswaldo

Reputation: 518

If there are any Angular users, there is a pipe to achieve this.

{{yourDate | date:'EEEE'}}

Upvotes: 1

Gabriel Kohen
Gabriel Kohen

Reputation: 4296

A much more elegant way which allows you to also show the weekday by locale if you choose to is available starting the latest version of ECMA scripts and is running in all latest browsers and node.js:

console.log(new Date().toLocaleString('en-us', {  weekday: 'long' }));

Upvotes: 112

Guga Nemsitsveridze
Guga Nemsitsveridze

Reputation: 751

If needed, the full name of a day ("Monday" for example) can be obtained by using Intl.DateTimeFormat with an options parameter.

var Xmas95 = new Date('December 25, 1995 23:15:30');

var options = { weekday: 'long'};
console.log(new Intl.DateTimeFormat('en-US', options).format(Xmas95));
// Monday
console.log(new Intl.DateTimeFormat('de-DE', options).format(Xmas95));
// Montag

Upvotes: 1

Arnold Ewin
Arnold Ewin

Reputation: 1183

This code is a modified version of what is given above. It returns the string representing the day instead

/**
* Converts a day number to a string.
*
* @param {Number} dayIndex
* @return {String} Returns day as string
*/
function dayOfWeekAsString(dayIndex) {
  return ["Sunday", "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][dayIndex] || '';
}

For example

dayOfWeekAsString(0) returns "Sunday"

Upvotes: 36

Tobias
Tobias

Reputation: 49

Just use the library moment.

const moment = require('moment')
console.log(moment(person.dateOfBirth,'DD.MM.YYYY').format("dddd {d}"))

Output:

Monday {1}

Upvotes: 1

Penny Liu
Penny Liu

Reputation: 17488

The first solution is very straightforward since we just use getDay() to get the day of week, from 0 (Sunday) to 6 (Saturday).

var dayOfTheWeek = (day, month, year) => {
  const weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  return weekday[new Date(`${month}/${day}/${year}`).getDay()];
};

console.log(dayOfTheWeek(3, 11, 2020));

The second solution is toLocaleString() built-in method.

var dayOfTheWeek = (day, month, year) => {
  return new Date(year, month - 1, day).toLocaleString("en-US", {
    weekday: "long",
  });
};

console.log(dayOfTheWeek(3, 11, 2020));

The third solution is based on Zeller's congruence.

function zeller(day, month, year) {
  const weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  if (month < 3) {
    month += 12;
    year--;
  }
  var h = (day + parseInt(((month + 1) * 26) / 10) +
    year + parseInt(year / 4) + 6 * parseInt(year / 100) +
    parseInt(year / 400) - 1) % 7;
  return weekday[h];
}
console.log(zeller(3, 11, 2020));

Upvotes: 4

Mohsen Alyafei
Mohsen Alyafei

Reputation: 5567

let weekday = ['Sunday',
               'Monday',
               'Tuesday',
               'Wednesday',
               'Thursday',
               'Friday',
               'Saturday'][new Date().getDay()];

console.log(weekday)

Upvotes: 1

Bakersen
Bakersen

Reputation: 71

let today= new Date()

//Function To Convert Day Integer to String

function daysToSrting() {
  const daysOfWeek = ['Sunday', 'Monday','Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  return daysOfWeek[today.getDay()]
}

console.log(daysToSrting())

Upvotes: 1

Raff
Raff

Reputation: 1058

This will add a getDayOfWeek() function as a prototype to the JavaScript Date class.

Date.prototype.getDayOfWeek = function(){   
    return ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][ this.getDay() ];
};

Upvotes: 9

Simon Bingham
Simon Bingham

Reputation: 1310

/**
* I convert a day string to an number.
*
* @method dayOfWeekAsInteger
* @param {String} day
* @return {Number} Returns day as number
*/
function dayOfWeekAsInteger(day) {
  return ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"].indexOf(day);
}

Upvotes: 9

j08691
j08691

Reputation: 208012

This will give you a day based on the index you pass:

var weekday=new Array(7);
weekday[0]="Monday";
weekday[1]="Tuesday";
weekday[2]="Wednesday";
weekday[3]="Thursday";
weekday[4]="Friday";
weekday[5]="Saturday";
weekday[6]="Sunday";
console.log("Today is " + weekday[3]);

Outputs "Today is Thursday"

You can alse get the current days index from JavaScript with getDay() (however in this method, Sunday is 0, Monday is 1, etc.):

var d=new Date();
console.log(d.getDay());

Outputs 1 when it's Monday.

Upvotes: 52

Related Questions