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()”.
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]]]]]);
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()
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>
Output result:
[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!