Days of the week are represented as three-letter strings javascript

Get the Day of the Week in JavaScript #

Call the getDay() method on the Date object to get the day of the week. The getDay method returns the day of the week for the specific date, represented as an integer between 0 and 6, where 0 is Sunday and 6 is Saturday.

Copied!

const dayOfWeekDigit = new Date().getDay(); console.log(dayOfWeekDigit); // 👉️ 0 const dayOfWeekName = new Date().toLocaleString( 'default', {weekday: 'long'} ); console.log(dayOfWeekName); // 👉️ Sunday

In the first example, we used the getDay method to get an integer from 0 to 6 that represents the day of the week for the supplied date.

Our second example uses the toLocaleString method to get the name of the day of the week.

We passed the following arguments to the method:

  1. the locale - in which language the name of the day should be returned. By specifying default, it can vary based on the user's browser preferences.
  2. the options object - we set the weekday setting to long to get the full name of the day. Other possible values are short and narrow.

If you want to get the day's name in a different locale, pass the locale as the first parameter to the method.

Copied!

const date = new Date(2027, 03, 24); // 👇️ Saturday console.log( date.toLocaleDateString('en-US', { weekday: 'long', }), ); // 👇️ Samstag console.log( date.toLocaleDateString('de-DE', { weekday: 'long', }), );

If you need to get the day's name in a different format, e.g. the first 3 letters or just the first letter, update the value of the weekday property in the options object.

Copied!

const date = new Date(2027, 03, 24); // 👇️ Saturday console.log( date.toLocaleDateString('en-US', { weekday: 'long', }), ); // 👇️ Sat console.log( date.toLocaleDateString('en-US', { weekday: 'short', }), ); // 👇️ S console.log( date.toLocaleDateString('en-US', { weekday: 'narrow', }), );

Setting the day to long gives us the entire name of the day. The short value gives us the first 3 letters of the day, and narrow - just the first letter.

Further Reading #

  • Get the first Day of the current Week in JavaScript
  • Format a Date as MM/DD/YYYY hh:mm:ss in JavaScript

Last update on August 19 2022 21:51:52 (UTC/GMT +8 hours)

JavaScript Datetime: Exercise-20 with Solution

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:-

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript function to get a textual representation of a day (three letters).</title>
</head>
<body>

</body>
</html>

JavaScript Code:

Date.shortDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

function short_Days(dt)
{ 
   return Date.shortDays[dt.getDay()];
}

dt = new Date();
console.log(short_Days(dt));

dt = new Date(2015, 10, 1);
console.log(short_Days(dt));

Sample Output:

Tue
Sun

Flowchart:

Days of the week are represented as three-letter strings javascript

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).

JavaScript: Tips of the Day

Imported module

// counter.js
let counter = 10;
export default counter;
// index.js
import myCounter from './counter';

myCounter += 1;

console.log(myCounter);

An imported module is read-only: you cannot modify the imported module. Only the module that exports them can change its value.
When we try to increment the value of myCounter, it throws an error: myCounter is read-only and cannot be modified.

Ref: https://bit.ly/323Y0P6