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.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
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('2022-4-10 11:53:04'); 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:
[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!