Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 657
dpmzmdr Avatar asked Jul 31 '13 07:07

dpmzmdr


People also ask

How do you get the day of the week from a date in JavaScript?

The getDay() method returns the day of the week (0 to 6) of a date.

How do you get day from date object?

Javascript date getDay() method returns the day of the week for the specified date according to local time. The value returned by getDay() is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.

How do you get day from string?

Translate the Date(). getDay() to a string day name : getDay « Date « JavaScript Tutorial. The getDay() method returns the day of the week expressed as an integer from 0 (Sunday) to 6 (Saturday). 12.6.

How do I get this week in JavaScript?

var today = new Date(); var startDay = 0; var weekStart = new Date(today. getDate() - (7 + today. getDay() - startDay) % 7); var weekEnd = new Date(today. getDate() + (7 - today.

How to get the weekday of a date in JavaScript?

The getDay () method returns the weekday of a date as a number (0-6): Example. var d = new Date (); document.getElementById("demo").innerHTML = d.getDay(); Try it Yourself ». In JavaScript, the first day of the week (0) means "Sunday", even if some countries in the world consider the first day of the week to be "Monday".

How to get the day name from a date string in Java?

We have a date in string format from which we want to get the day name. First, we convert the date string to the date object and then use the getDay () method. We get the index of day.

How to get the day of the week of a date?

The getDay () method returns the day of the week (0 to 6) of a date. Sunday = 0, Monday = 1, ... (See below): A number. The day of the week (0 to 6).

What is getday () method in JavaScript?

JavaScript getDay () Method 1 Definition and Usage. The getDay () method returns the day of the week (from 0 to 6) for the specified date. ... 2 Browser Support 3 Syntax 4 Parameters 5 Technical Details 6 More Examples 7 Related Pages


3 Answers

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.

like image 128
Samuel Liew Avatar answered Oct 14 '22 23:10

Samuel Liew


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

Docs for Intl.DateTimeFormat object

Docs for localeDateString

like image 30
Black Mamba Avatar answered Oct 14 '22 21:10

Black Mamba


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
like image 21
Code Lღver Avatar answered Oct 14 '22 21:10

Code Lღver