html页面嵌入时间和日期

先给个script:

<script type="text/javascript">
function getNowDate() {
var date = new Date();
var year = date.getFullYear() //
var month = date.getMonth() + 1; //
var day  = date.getDate(); //
var hour = date.getHours(); //
var minutes = date.getMinutes(); //
var seconds = date.getSeconds() //
var weekArr = ['星期日','星期一', '星期二', '星期三', '星期四', '星期五', '星期六' ];
var week = weekArr[date.getDay()];
// 给一位数数据前面加 “0”
if (month >= 1 && month <= 9) {
 month = "0" + month;
}
if (day >= 0 && day <= 9) {
 day = "0" + day;
}
if (hour >= 0 && hour <= 9) {
 hour = "0" + hour;
}
if (minutes >= 0 && minutes <= 9) {
 minutes = "0" + minutes;
}
if (seconds >= 0 && seconds <= 9) {
 seconds = "0" + seconds;
}
document.getElementById("info").innerHTML = year + "" + month + "" + day + "" + " " + week;
//return currentdate;
}

</script>

然后找到对应添加时间的位置

<div class="fl">
<span id="info"></span>
</div>

最后使他页面加载的时候就开始读取:

<body οnlοad="getNowDate()">

原文链接:https://blog.csdn.net/copyerman115/article/details/78584715

 

posted @ 2020-02-25 20:36  努力乄小白  阅读(5861)  评论(0编辑  收藏  举报