Get the weekday from a Date object or date string using JavaScript
Asked Answered
V

4

30

I have a date string in (yyyy-mm-dd) format, how can I get the weekday name from it?

Example:

  • For the string "2013-07-31", the output would be "Wednesday"
  • For today's date using new Date(), the output would be based on the current day of week
Valuate answered 31/7, 2013 at 7:28 Comment(0)
E
38

Use this function, comes with date string validation:

If you include this function somewhere in your project,

// Accepts a Date object or date string that is recognized by the Date.parse() method
function getDayOfWeek(date) {
  const dayOfWeek = new Date(date).getDay();    
  return isNaN(dayOfWeek) ? null : 
    ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][dayOfWeek];
}

You will be able to use it anywhere easily like this:

getDayOfWeek( "2013-07-31" )
> "Wednesday"

getDayOfWeek( new Date() ) // or
getDayOfWeek( Date.now() )
> // (will return today's day. See demo jsfiddle below...)

If invalid date string is used, a null will be returned.

getDayOfWeek( "~invalid string~" );
> null

Valid date strings are based on the Date.parse() method as described in the MDN JavaScript reference.

Demo: http://jsfiddle.net/samliew/fo1nnsgp/


Of course you can also use the moment.js plugin, especially if timezones are required.

Edithe answered 31/7, 2013 at 7:39 Comment(0)
V
15

Here are one-liner solutions but please check the support first.

let current = new Date();
let today = current.toLocaleDateString('en-US',{weekday: 'long'});
console.log(today);

let today2 = new Intl.DateTimeFormat('en-US', {weekday: 'long'}).format(current);
console.log(today2)

Docs for Intl.DateTimeFormat object

Docs for localeDateString

Varietal answered 21/7, 2018 at 19:43 Comment(0)
S
12

Use below code:

var gsDayNames = [
  'Sunday',
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday',
  'Saturday'
];

var d = new Date("2013-07-31");
var dayName = gsDayNames[d.getDay()];
//dayName will return the name of day
Skyway answered 31/7, 2013 at 7:38 Comment(0)
C
0

find all weekdays from current days.

let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
let dates = [];

days.forEach((day,index)=>{
    if(index == new Date().getDay()){
            for(let i = index-1; i >= 0; i--) {
            // console.log("days is "+day+" => "+(new Date().getDate() - i))
        var date = new Date();
        dates.push({date: new Date(date.setDate(date.getDate() - i)).toString(), days: days[i]+"-"+i})
    }
        var date = new Date();
        dates.push({date: new Date(date.setDate(date.getDate())).toString(), days: days[index]+"-"+index})
    // console.log("Today is "+day+" => "+new Date().getDate())
            for(let i = index+1; i < days.length; i++) {
            // console.log("day is "+day+" => "+(new Date().getDate() + i))
        var date = new Date();
                    // add a day
        dates.push({date: new Date(date.setDate(date.getDate() + i)).toString(), days: days[i]+"-"+i})
        }
    }
})

console.log(dates)
Christopher answered 16/10, 2023 at 5:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.