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

How to calculate the current day of the week in JavaScript

青灯夜游
Release: 2022-01-18 18:59:32
Original
9063 people have browsed it

Method: 1. Define the array of "array("日","一","二","三","四","五","六")"; 2. Use Date The getDay() method of the object obtains the number representing the current week; 3. Use the number as a subscript and retrieve the corresponding week value in the array.

How to calculate the current day of the week in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript calculates the current day of the week

<!doctype html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>获取当前星期</title>
 </head>
 <body>
<script type="text/javascript">
    /**
     *获取当前星期几
     *
    */
    function getWeekDate() {
var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
var now = new Date();
var day = now.getDay();
var week = weeks[day];
return week;
    }
    alert(getWeekDate());
</script>
 </body>
</html>
Copy after login

Output result:

How to calculate the current day of the week in JavaScript

How to calculate the current day of the week in JavaScript

Description:

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

dateObject.getDay()
Copy after login

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

So if you need to accurately give the current day of the week, you need to define an Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ", "Saturday") array.

Then use the number returned by getDay() as a subscript and take out the corresponding week value in the array.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to calculate the current day of the week in 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