w3resource

JavaScript: Get a full textual representation of the day of the week

JavaScript Datetime: Exercise-21 with Solution

Full Weekday Name

Write a JavaScript function to get a full textual representation of the weekday (Sunday through Saturday).

Test Data:
dt = new Date(2015, 10, 1);
console.log(long_Days(dt));
"Sunday"

Sample Solution:

JavaScript Code:

// Define a property 'longDays' on the Date object to store an array of long day names
Date.longDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

// Define a JavaScript function called long_Days with parameter dt (date)
function long_Days(dt)
{ 
  // Return the long day name corresponding to the day of the week of the provided date
  return Date.longDays[dt.getDay()]; 
}

// Create a new Date object representing the current date
dt = new Date();
// Output the long day name for the current date
console.log(long_Days(dt));

// Create a new Date object representing November 1, 2015
dt = new Date(2015, 10, 1);
// Output the long day name for November 1, 2015
console.log(long_Days(dt));

Output:

Tuesday
Sunday

Explanation:

In the exercise above,

  • The code defines a property 'longDays' on the "Date" object, which is an array containing full day names ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday').
  • It then defines a JavaScript function named "long_Days()" with one parameter 'dt', representing a Date object.
  • Inside the long_Days function:
    • It retrieves the day of the week (0 for Sunday, 1 for Monday, ..., 6 for Saturday) from the provided Date object 'dt' using the "getDay()" method.
    • It uses this day of the week as an index to access the corresponding full day name from the 'Date.longDays' array.
    • It returns the full day name.
  • The code then demonstrates the usage of the "long_Days()" function:
    • It creates a new Date object 'dt' representing the current date (new Date()).
    • It outputs the full day name for the current date by calling the "long_Days()" function with 'dt' and logging the result to the console.
    • It creates another new Date object 'dt' representing November 1, 2015 (new Date(2015, 10, 1)).
    • It outputs the full day name for November 1, 2015 by calling the "long_Days()" function with 'dt' and logging the result to the console.

Flowchart:

Flowchart: JavaScript- Get a full textual representation of the day of the week

Live Demo:

See the Pen JavaScript - Get a full textual representation of the day of the week-date-ex-21 by w3resource (@w3resource) on CodePen.


For more Practice: Solve these Related Problems:

  • Write a JavaScript function that returns the full name of the weekday from a Date object using an array of full day names.
  • Write a JavaScript function that converts a numeric day from getDay() into its full weekday name.
  • Write a JavaScript function that accepts a date string and outputs the full weekday name after conversion to a Date object.
  • Write a JavaScript function that supports locale-specific weekday names based on a provided locale code.

Go to:


PREV : Short Day Name.
NEXT : ISO Weekday Number.

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.