<script>
var date = new Date();
//年月日
console.log(date)
var year = date.getFullYear();
console.log(year)
//月份从0开始
var month = date.getMonth()+1;
console.log(month)
var data = date.getDate();
console.log(data)
时间表
p>14:14:10</p>
<script>
var d= new Date();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
console.log(hours)
console.log(minutes)
console.log(seconds)
</script>
当前时间在html页面上
<div id="app"></div>
<!-- 当前时间页面 -->
<script>
setInterval(show,1000)
function show(){
var app = document.getElementById("app")
var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var sum = handleNum(hours)+":"+handleNum(minutes)+":"+handleNum(seconds);
console.log(sum)
app.innerHTML = sum
function handleNum(num){
if(num<10){
num = "0"+num;
setTimeout.d
}
return num;
}
}
</script>
算时间
<script>
var time = "2021-10-13 17:10:10"
/*
多少个小时,多少分钟,多少秒
1获取当前时间的毫秒数
*/
var d = new Date();
//console.log(d.getTime())
//2获取活动开始时间的毫秒数
var startTime = d.getTime
var endTime = new Date(time).getTime();
//console.log(endTime)
//3得到中间间隔的毫秒数
var gapTime = endTime-startTime;
//console.log(gapTime)
var seconds = Math.floor(gapTime/1000);
console.log(seconds)
var hours = Math.floor(seconds/3600);
console.log(hours)
seconds = seconds -3600*hours;
var minutes = Math.floor(seconds/60);
console.log(minutes)
seconds = seconds-minutes*60;
console.log(seconds)
</script>
<script>
var time = "2021-10-13 17:10:10"
/*
多少个小时,多少分钟,多少秒
1获取当前时间的毫秒数
*/
var d = new Date();
//console.log(d.getTime())
//2获取活动开始时间的毫秒数
var startTime = d.getTime
var endTime = new Date(time).getTime();
//console.log(endTime)
//3得到中间间隔的毫秒数
var gapTime = endTime-startTime;
//console.log(gapTime)
var seconds = Math.floor(gapTime/1000);
console.log(seconds)
var hours = Math.floor(seconds/3600);
console.log(hours)
seconds = seconds -3600*hours;
var minutes = Math.floor(seconds/60);
console.log(minutes)
seconds = seconds-minutes*60;
console.log(seconds)
</script>
<script>
var time = 306000;
//3000毫秒
var seconds = time/1000;
console.log(seconds+"秒")
var seconds = seconds-minutes*60
//几分钟
var minutes =Math.floor(seconds/60);
console.log(minutes+"分")
//3s
console.log(seconds+"余下的秒")
<script>
var time = 306050000;
/*
3000
1m =60s
1h = 60*60s
*/
var seconds = time/1000;
console.log(seconds+"秒")
var hours =Math.floor(seconds/3600);
console.log(hours+"小时")
var seconds = seconds%3600;
console.log(seconds)
var minutes =Math.floor(seconds/60);
console.log(minutes)
seconds =seconds%60;
console.log(seconds)
</script>
<script>
var time = "2021-10-13 21:30:00"
var d = new Date();
//console.log(d.getTime())
var startTime = d.getTime();
//获取活动开始时间的毫秒数
var endTime = new Date(time).getTime();
//console.log(endTime)
//3 得到中间间隔的毫秒数
var gapTime = endTime -startTime;
var seconds = Math.floor(gapTime/1000)
console.log(seconds+"秒")
var hours = Math.floor(seconds/3600);
console.log(hours)
seconds = seconds - 3600*hours;
var minutes = Math.floor(seconds/60);
console.log(minutes)
seconds = seconds -minutes*60;
console.log(seconds)
</script>
<script>
var time = "2021-10-19 08:30:00"
var startTime = (new Date()).getTime();
//console.log(startTime)
var endTime = (new Date(time)).getTime();
var gapTime = endTime-startTime;
//console.log(gapTime/1000/60)
var minutes =Math.floor(gapTime/(1000*60));
//console.log(minutes)
/*
8201分
1h =60m
1t = 60*24
*/
var days =Math.floor(minutes/(60*24));
minutes = minutes -days*(60*24)
console.log(days)
var hours = Math.floor(minutes/60)
console.log(hours)
minutes = minutes-hours*60;
console.log(minutes)
</script>