Home  >  Article  >  Web Front-end  >  How to output the day of the week in JavaScript

How to output the day of the week in JavaScript

WBOY
WBOYOriginal
2021-11-11 18:30:424090browse

Method: 1. Define the "Array("Day", "One", "Two", "Three", "Four", "Five", "Six")" array; 2. Use getDay( ) method to obtain the number representing the day of the week; 3. Use the number as a subscript to obtain the value in the array; 4. Output through "document.write".

How to output the day of the week in JavaScript

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

How to output the week in JavaScript:

In the js tag, use the getDay() method to get today’s week code from the Date object . Then the Chinese name of the week is obtained by encoding the week in the array.

The getDay() method can return the number of a certain day of the week (0~6). Sunday is 0, Monday is 1, and so on.

JavaScript's Date object is used to process dates and times.

The code is as follows:

new Date().getDay()
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script >
var a = new Array("日", "一", "二", "三", "四", "五", "六");  
var week = new Date().getDay();  
var str = "今天是星期"+ a[week];  
alert(str); 
</script>
</body>
</html>

Output result:

How to output the day of the week in JavaScript

The above example is in the form of a pop-up window, of course it can also be through a document. Write direct output:

How to output the day of the week in JavaScript

#For more programming-related knowledge, please visit: Programming Video! !

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