js获取当前时间星期几
时间: 2023-09-21 18:03:43 浏览: 158
在JavaScript中,可以使用Date对象的getDay()方法来获取当前时间是星期几。这个方法返回一个数字,对应的是0到6,其中0代表星期日,1代表星期一,以此类推。你可以通过使用switch语句或者数组来将数字转换为对应的星期几文本。
以下是两种常见的方法:
方法一:使用switch语句
```
var tempDate = new Date();
var days = tempDate.getDay();
var week;
switch(days) {
case 0: week = '星期日'; break;
case 1: week = '星期一'; break;
case 2: week = '星期二'; break;
case 3: week = '星期三'; break;
case 4: week = '星期四'; break;
case 5: week = '星期五'; break;
case 6: week = '星期六'; break;
}
```
方法二:使用数组
```
var tempDate = new Date();
var days = tempDate.getDay();
var weekArray = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
var week = weekArray<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [js获取当前星期几](https://blog.csdn.net/LONG_Yi_1994/article/details/110796373)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [【JS新手教程】JS获取当前星期几的几种方法](https://blog.csdn.net/weixin_30835933/article/details/98309459)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文