Home > Web Front-end > JS Tutorial > body text

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

黄舟
Release: 2017-11-04 14:23:46
Original
3617 people have browsed it

Definition and Usage

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

Syntax

dateObject.getDay()
Copy after login

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>
Copy after login

Output:

6
Copy after login

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>
Copy after login

Output:

Today it is Saturday
Copy after login

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>
Copy after login

This will produce the following results:

getDay() : 1
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template