How to get today's day of the week with JavaScript

WBOY
Release: 2022-01-17 15:50:46
Original
3804 people have browsed it

How to get today’s day of the week in JavaScript: 1. Use the new keyword to define a Date object, the syntax is “new Date()”; 2. Use the getDay() method to get today’s day of the week through the defined Date object That’s it, the syntax is “Date.getDay()”.

How to get today's day of the week with JavaScript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to get today’s day of the week in JavaScript

The Date object is used to process date and time.

Date objects can be defined through the new keyword. The following code defines a Date object named myDate:

There are four ways to initialize the date:

new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
Copy after login

The getDay() method can return the number of a certain day of the week (0~6).

Note: Sunday is 0, Monday is 1, and so on.

The syntax is:

Date.getDay()
Copy after login

The example is as follows:

<html>
<head>
<meta charset="utf-8">
<title>123</title>
</head>
<body>
<p id="demo">单击按钮显示一周中的今天</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
var d = new Date();
var x = document.getElementById("demo");
x.innerHTML="星期"+d.getDay();
}
</script>
</body>
</html>
Copy after login

Output result:

How to get todays day of the week with JavaScript

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to get today's day of the week with JavaScript. 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