w3resource

JavaScript: Get a textual representation of a day

JavaScript Datetime: Exercise-20 with Solution

Short Day Name

Write a JavaScript function to get a textual representation of a day (three letters, Mon through Sun).

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

Sample Solution:

JavaScript Code:

// Define a property 'shortDays' on the Date object to store an array of short day names
Date.shortDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

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

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

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

Output:

Tue
Sun

Explanation:

In the exercise above,

  • The code defines a property 'shortDays' on the "Date" object, which is an array containing abbreviated day names ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat').
  • It then defines a JavaScript function named 'short_Days' with one parameter 'dt', representing a Date object.
    • Inside the "short_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 abbreviated day name from the 'Date.shortDays' array.
    • It returns the abbreviated day name.
  • The code then demonstrates the usage of the "short_Days()" function:
    • It creates a new Date object "dt" representing the current date (new Date()).
    • It outputs the abbreviated day name for the current date by calling the "short_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 abbreviated day name for November 1, 2015 by calling the short_Days function with dt and logging the result to the console.

Flowchart:

Flowchart: JavaScript- Get a textual representation of a day

Live Demo:

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


Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript function to get the day of the month, 2 digits with leading zeros.
Next: Write a JavaScript function to get a full textual representation of the day of the week (Sunday through Saturday).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/javascript-date-exercise-20.php