Home  >  Article  >  Web Front-end  >  How to use JavaScript’s getday method

How to use JavaScript’s getday method

藏色散人
藏色散人Original
2021-11-08 14:21:124783browse

JavaScript's getday method is used to specify the day of the week. Its usage syntax is "Date.getDay()". If the return value is 0, it means Sunday, if it is 1, it means Monday, and so on.

How to use JavaScript’s getday method

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

How to use JavaScript’s getday method?

getDay() method returns the day of the week (from 0 to 6) for the specified date.

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

Syntax

Date.getDay()

Parameters: No parameters.

Return value: Numeric value, from 0 to 6, indicating the day of the week.

Code example:

<!DOCTYPE html>
<html>
<body>

<p>单击该按钮可显示今天是星期几。</p>

<button onclick="myFunction()">试一试</button>

<p id="demo"></p>

<script>
function myFunction() {
  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";

  var n = weekday[d.getDay()];
  document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

[Recommended learning: javascript basic tutorial]

The above is the detailed content of How to use JavaScript’s getday method. 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