Home  >  Article  >  Web Front-end  >  JavaScript method getDay() returns the day of the week (0 ~ 6) from the Date object

JavaScript method getDay() returns the day of the week (0 ~ 6) from the Date object

黄舟
黄舟Original
2017-11-04 14:23:463610browse

Definition and Usage

getDay() method returns a number representing a certain day of the week.

Syntax

dateObject.getDay()

Return value

The day of the week pointed to by dateObject, using local time. The return value is an integer between 0 (Sunday) and 6 (Saturday).

Tips and Notes:

Note: This method is always used in conjunction with a Date object.

Example

Example 1

In this example, we will get the current day of the week as a number:

<script type="text/javascript">

var d=new Date()
document.write(d.getDay())

</script>

Output:

6

Example 2

Now, we will create an array so that the above example outputs the name of the week instead of a number:

<script type="text/javascript">

var d=new Date()

var weekday=new Array(7)
weekday[0]="Sunday"
weekday[1]="Monday"
weekday[2]="Tuesday"
weekday[3]="Wednesday"
weekday[4]="Thursday"
weekday[5]="Friday"
weekday[6]="Saturday"

document.write("Today it is " + weekday[d.getDay()])

</script>

Output:

Today it is Saturday

Returns the day of the week to the specified date according to local time.
Example:

<html>
<head>
<title>JavaScript getDay Method</title>
</head>
<body>
<script type="text/javascript">
  var dt = new Date("December 25, 1995 23:15:00");
  document.write("getDay() : " + dt.getDay() ); 
</script>
</body>
</html>

This will produce the following results:

getDay() : 1

The above is the detailed content of JavaScript method getDay() returns the day of the week (0 ~ 6) from the Date object. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn