I have a date string in (yyyy-mm-dd) format, how can I get the weekday name from it?
Example:
new Date()
, the output would be based on the current day of weekThe getDay() method returns the day of the week (0 to 6) of a date.
Javascript date getDay() method returns the day of the week for the specified date according to local time. The value returned by getDay() is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
Translate the Date(). getDay() to a string day name : getDay « Date « JavaScript Tutorial. The getDay() method returns the day of the week expressed as an integer from 0 (Sunday) to 6 (Saturday). 12.6.
var today = new Date(); var startDay = 0; var weekStart = new Date(today. getDate() - (7 + today. getDay() - startDay) % 7); var weekEnd = new Date(today. getDate() + (7 - today.
The getDay () method returns the weekday of a date as a number (0-6): Example. var d = new Date (); document.getElementById("demo").innerHTML = d.getDay(); Try it Yourself ». In JavaScript, the first day of the week (0) means "Sunday", even if some countries in the world consider the first day of the week to be "Monday".
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.
The getDay () method returns the day of the week (0 to 6) of a date. Sunday = 0, Monday = 1, ... (See below): A number. The day of the week (0 to 6).
JavaScript getDay () Method 1 Definition and Usage. The getDay () method returns the day of the week (from 0 to 6) for the specified date. ... 2 Browser Support 3 Syntax 4 Parameters 5 Technical Details 6 More Examples 7 Related Pages
If you include this function somewhere in your project,
// Accepts a Date object or date string that is recognized by the Date.parse() method
function getDayOfWeek(date) {
const dayOfWeek = new Date(date).getDay();
return isNaN(dayOfWeek) ? null :
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][dayOfWeek];
}
You will be able to use it anywhere easily like this:
getDayOfWeek( "2013-07-31" )
> "Wednesday"
getDayOfWeek( new Date() ) // or
getDayOfWeek( Date.now() )
> // (will return today's day. See demo jsfiddle below...)
If invalid date string is used, a null will be returned.
getDayOfWeek( "~invalid string~" );
> null
Valid date strings are based on the Date.parse() method as described in the MDN JavaScript reference.
Demo: http://jsfiddle.net/samliew/fo1nnsgp/
Of course you can also use the moment.js plugin, especially if timezones are required.
Here are one-liner solutions but please check the support first.
let current = new Date();
let today = current.toLocaleDateString('en-US',{weekday: 'long'});
console.log(today);
let today2 = new Intl.DateTimeFormat('en-US', {weekday: 'long'}).format(current);
Docs for Intl.DateTimeFormat object
Docs for localeDateString
Use below code:
var gsDayNames = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
];
var d = new Date("2013-07-31");
var dayName = gsDayNames[d.getDay()];
//dayName will return the name of day
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With