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
?
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
?
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' }));
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.
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"
/**
* 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);
}
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() ];
};
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));
const language = 'en-us';
const options = { weekday: 'long' };
const today = new Date().toLocaleString(language, options)
cosnsole.log(today)
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())
let weekday = ['Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'][new Date().getDay()];
console.log(weekday)
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
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.
If there are any Angular users, there is a pipe to achieve this.
{{yourDate | date:'EEEE'}}
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';
}
}