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.
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>
Output result:
Description:
getDay() method can return a number representing a certain day of the week.
dateObject.getDay()
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!