w3resource

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

JavaScript Datetime: Exercise-21 with Solution

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.


Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript function to get a textual representation of a day (three letters, Mon through Sun).
Next: Write a JavaScript function to get ISO-8601 numeric representation of the day of the week (1 (for Monday) to 7 (for Sunday)).

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.