javascript
Get the day name from given date in Javascript
You can get the day name from a date using javascript. Below are some code examples that will show you how to get the day name from a date string to a date.
const date_str = "07/20/2021";
const date = new Date(date_str);
const full_day_name = date.toLocaleDateString('default', { weekday: 'long' });
// -> to get full day name e.g. Tuesday
const short_day_name = date.toLocaleDateString('default', { weekday: 'short' });
console.log(short_day_name);
// -> TO get the short day name e.g. Tue
In the above code snippet, we are using .toLocaleDateString() method of javascript date object. We are passing 'default' as the language code in which we want to access the day name and the weekday key is assigned 'long' or 'short' value.
Live Demo
const days_name = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const date_str = "03/17/2021"; // mm/dd/yyyy
const date_obj = new Date(date_str);
const day_name = days_name[date_obj.getDay()];
console.log(day_name);
In the above code, we have defined days' names in an array. We have a date in string format from which we want to get the day name. First, we convert the date string to the date object and then use the getDay() method. We get the index of day.
var dateStr = '07/16/2019';
var date = new Date(dateStr);
console.log(date.toLocaleDateString("en-US", { weekday: 'long' }));
// -> Tuesday
console.log(date.toLocaleDateString("hi-IN", { weekday: 'long' }));
// -> ???????
console.log(date.toLocaleDateString("de", { weekday: 'long' }));
// -> Dienstag
console.log(date.toLocaleDateString("zh-CN", { weekday: 'long' }));
// -> ???
console.log(date.toLocaleDateString("ar-om", { weekday: 'long' }));
// -> ????????
console.log(date.toLocaleDateString("ru-RU", { weekday: 'long' }));
// -> ???????
console.log(date.toLocaleDateString("ko-KR", { weekday: 'long' }));
// -> ???
Was this helpful?
Similar Posts
- Get year from given date using Javascript
- Get month name from a date using javascript
- Check if given value is an array in Javascript
- Get current year using Javascript Date getFullYear() method
- Get the current month name using javascript
- Javascript function to get cookie value by name
- Get Tag Name of an Element using Javascript