Home  >  Article  >  Web Front-end  >  How to get the day of the week a certain day is in Javascript

How to get the day of the week a certain day is in Javascript

WBOY
WBOYOriginal
2022-04-11 14:33:547525browse

Method: 1. Use "Date('Year-Month-Day Hour:Minute:Second');" to obtain the date object of the specified time; 2. Use "date object.getDay();" to obtain the specified time The time represents the number of a certain day of the week; 3. Use the obtained number as a subscript to retrieve the day of the week in the specified array.

How to get the day of the week a certain day is in Javascript

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

How to get the day of the week in Javascript

The Date object is used to process date and time.

Create a Date object: new Date()

The following four methods can also create a Date object:

var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

The getDay() method can return a week (0~6) Number for a certain day.

The syntax is:

Date.getDay()

The return value is an integer between 0 (Sunday) and 6 (Saturday).

The example is as follows:

<!DOCTYPE html>
<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(&#39;2022-4-10 11:53:04&#39;);
var x = d.getDay();
var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
var week = weeks[x];
var y = document.getElementById("demo");
y.innerHTML=week;
}
</script>
</body>
</html>

Output result:

How to get the day of the week a certain day is in Javascript

[Related recommendations: javascript video tutorial, webfrontend

The above is the detailed content of How to get the day of the week a certain day is in Javascript. 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