getDay() method. This method is part of the Date object and is used to retrieve the day of the week for a specific date.
What is getDay() Method?
var d = new Date(); var n = d.getDay();
In this example, ‘d’ represents a new Date object while ‘n’ will hold the value returned by
To understand how to use this function properly, let’s look at an example:
var d = new Date(); console.log("Today is: " + d.getDay());
This code will output a number representing today’s day of the week. However, it might be more useful to have this information in a human-readable format. We can achieve this by creating an array of weekday names and using
getDay() as an index:
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; var d = new Date(); console.log("Today is: " + days[d.getDay()]);
Tips & Tricks
getDay()method does not take any parameters.
- This method will return a value between 0 (Sunday) and 6 (Saturday).
- If you want to get the day of the month instead, use the
getDay() method is a powerful tool for working with dates. Whether you’re building a calendar app or just need to display the current day on your website, this function can come in handy. Remember that it returns a number representing the day of the week, so you may need to convert this into a more readable format for your users.
getDay() method effectively!