js获取当前时间年月日星期
时间: 2023-11-08 21:56:14 浏览: 156
可以使用以下两种方法来使用JavaScript获取当前时间的年月日星期:
方法一:
```javascript
<script type="text/javascript">
function getCurrentDate() {
var now = new Date();
var year = now.getFullYear(); //得到年份
var month = now.getMonth()+1;//得到月份
var date = now.getDate();//得到日期
var day = now.getDay();//得到周几
var week;
var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
week = arr_week[day];
var time = "";
time = "当前时间:" + year + "年" + month + "月" + date + "日" + " " + week;
document.write(time);
}
</script>
```
方法二:
```javascript
<script>
var now = new Date();
var year = now.getFullYear(); //得到年份
var month = now.getMonth()+1;//得到月份
var date = now.getDate();//得到日期
var day = now.getDay();//得到周几
var week;
var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
week = arr_week[day];
var time = "";
time = "当前时间:" + year + "年" + month + "月" + date + "日" + " " + week;
document.write(time);
</script>
```
阅读全文