获取时间
<body>
<p id="count"></p>
<script>
var count = document.getElementById('count')
function time(){
var date = new Date();
console.log(date)
var arr = ['周日','周一','周二','周三','周四','周五','周六',]
var week = date.getDay()//从周日~周六 0~6
console.log(arr[week])
var year = date.getFullYear()
console.log(year)
var month = date.getMonth()+1
console.log(month)//月份 0~11 计数
var day = date.getDate()
console.log(day)
var hours = date.getHours()
console.log(hours)
var mins = date.getMinutes()
console.log(mins)
var sec = date.getSeconds()
console.log(sec)
var i = date.getMilliseconds()//毫秒
console.log(i)
console.log(date.getTime())//时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
count.innerHTML = '当前时间:'+year+'年'+bl(month)+'月'+bl(day)+'日'+arr[week]+bl(hours)+'时'+bl(mins)+'分'+bl(sec)+'秒'
}
time()
console.log(time())
var timer = setInterval(function(){
time()
}, 1000);
function bl(x){
return x<10?'0'+x:x
}
</script>
</body>
倒计时
<body>
<p id="count"></p>
<script>
var count = document.querySelector('#count')
function djs(a,b,c,d,e,f,g){
var startDate = new Date()
// var endDate = new Date(2019,8,15,12,0,0)
var endDate = new Date(a,b-1,c,d,e,f)
var time = parseInt((endDate - startDate)/1000)
console.log(time)
var d = parseInt(time/60/60/24)
var h = parseInt(time/60/60%24)
var m = parseInt(time/60%60)
var s = parseInt(time%60)
count.innerHTML = g+d+'天'+h+'时'+m+'分'+s+'秒'
}
// djs()
djs(2019,08,15,12,0,0,'距离还有:')
var timer = setInterval(function(){
// djs()
djs(2019,08,15,12,0,0,'距离还有:')
},1000)
setInterval(djs,1000,'2020,1,1 00:00:00','距离元旦:')
</script>
</body>