54

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?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
  • possible duplicate of [how to get the day from a particular date using javascript](http://stackoverflow.com/questions/495644/how-to-get-the-day-from-a-particular-date-using-javascript) – mechanical_meat Mar 13 '12 at 02:54
  • [MDN Date()](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) – epascarello Mar 13 '12 at 02:55
  • possible duplicate of [Javascript: How to get the day of week and the month of the year?](http://stackoverflow.com/questions/4822852/javascript-how-to-get-the-day-of-week-and-the-month-of-the-year) – Darius Kucinskas Oct 25 '13 at 07:32

14 Answers14

104

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' }));
Luke Vanzweden
  • 466
  • 3
  • 15
Gabriel Kohen
  • 4,166
  • 4
  • 31
  • 46
  • 4
    Just a warning to anyone using this, it works well, but this is a very heavy way to get this. Only use it when you have few dates that you need to convert and not on a sever side nodejs application. If you run this on 10k dates, it takes a long time and there's a memory leak. – Omer Jun 26 '17 at 17:30
  • 1
    @omer, did you happen to open an issue with the node team? I think we can all benefit from this working faster and with no memory leaks. – Gabriel Kohen Jun 26 '17 at 17:59
  • 1
    No I didn't yet but I will open it now, good idea. I managed to reproduce it with a simple while loop that runs 10k times. I am not sure if it's actually a bug or just that this function is heavy on resources for this specific use case. – Omer Jun 27 '17 at 08:01
  • 2
    For performance, MDN authors recommend: "When formatting large numbers of dates, it is better to create an Intl.DateTimeFormat object and use the function provided by its format property." Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString – sean Sep 07 '17 at 11:02
  • 1
    Might want to use `navigator.language` instead of `'en-us'`. So the user gets her Mondays in the language of her choosing. – Bitterblue Dec 05 '19 at 10:01
  • @sean The performance impact has either been addressed or is now negligible. The aforementioned tip for formatting by MDN has since been removed, although it exists in other versions like [german](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString#performance). For Chromium-based browsers and therefore V8, I think it's a ghost from the past, see: https://v8.dev/blog/intl – Rick Stanley Jul 15 '21 at 01:04
51

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.

j08691
  • 204,283
  • 31
  • 260
  • 272
  • 6
    `var dayOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];` – Zei May 02 '21 at 21:53
35

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"
Arnold Ewin
  • 1,113
  • 1
  • 13
  • 26
  • 8
    This is incorrect since `getDay()` implementations starts on `Sunday`. It should be instead: return ["Sunday","Monday", "Tuesday","Wednesday","Thursday","Friday","Saturday"][dayIndex]; – Germain Nov 11 '19 at 10:35
9
/**
* 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);
}
Simon Bingham
  • 1,205
  • 11
  • 15
9

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() ];
};
Raff
  • 828
  • 10
  • 16
4

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));
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
3
const language = 'en-us';
const options = {  weekday: 'long' };
const today = new Date().toLocaleString(language, options)
cosnsole.log(today)
Ali Raza
  • 1,026
  • 13
  • 20
1
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())
Bakersen
  • 41
  • 4
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. [From Review](/review/low-quality-posts/26280394) – double-beep May 31 '20 at 14:48
1

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

console.log(weekday)
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42
1

Just use the library moment.

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

Output:

Monday {1}
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Tobias
  • 39
  • 4
1

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
Guga Nemsitsveridze
  • 721
  • 2
  • 7
  • 27
1

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.

Mauricio Moraes
  • 7,255
  • 5
  • 39
  • 59
Watchmaker
  • 4,908
  • 1
  • 37
  • 38
1

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

{{yourDate | date:'EEEE'}}
Kevin Oswaldo
  • 478
  • 5
  • 13
-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';
  }
}
Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • Hello, please see https://meta.stackoverflow.com/editing-help Thanks! – Eric Aya Oct 25 '22 at 15:32
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 28 '22 at 18:01
  • days of the week in JS are 0-6. NOT 1-7. This is off by 1 – Rilcon42 Jan 31 '23 at 22:15