<script>
var date= new Date();
/* 年 月 日 */
console.log(date)
var year = date.getFullYear();
console.log(year)
/* 月份从0开始,要加1 */
var month = date.getMonth()+1;
console.log(month)
var data = date.getDate();
console.log(data)
</script>
返回当前时间:
<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>
输出实时时间(秒可以变化):
<body>
<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;
}
return num;
}
</script>
</body>
距离活动开始时间:小时分钟秒
<script>
var time = "2021-10-13 17:10:10"
/* 多个小时,多分钟,多少秒 */;
/* 1、获取当前时间的毫秒数 */
var d = new Date();
// console.log(d.getTime())
var startTime = d.getTime();
/* 2、获取活动开始时间的毫秒 */
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)
/* var time = "2021-10-13 21:30:00" 第一个需求*/
/* var time ="2021-10-19 08:30:00" 还有几天几小时几分 */
</script>
把给定的time时间(毫秒)转换为几小时几分钟几秒:
<script>
var time = 360000;
/*
66s
*/
/*
一秒
60s 一分钟
60*60 一小时
time/1000 秒
*/
var seconds = Math.floor(time/1000);
console.log(seconds)
var hours = Math.floor(seconds/3600);
console.log(hours)
// seconds = seconds- hours*3600;
seconds = seconds%3600;
console.log(seconds)
var minutes = Math.floor(seconds/60);
console.log(minutes)
seconds = seconds- minutes*60;
console.log(seconds)
</script>
<script>
var time = 306000;
/* 3000
1m = 60s
*/
var seconds = time/1000;
console.log(seconds+"秒")
var minutes = Math.floor(seconds/60);
var seconds = seconds - minutes*60;
console.log(minutes+"分")
console.log(seconds+"余下的秒")
/* 3s */
</script>
<script>
var time = 306150000;
/* 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)
/* 3s */
</script>
现在距离10.19还有几天+几小时+几分钟:
<script>
/* var time ="2021-10-19 08:30:00" 还有几天几小时几分 */
var time = "2021-10-19 08:30:00"
var startTime = (new Date()).getTime();
var endTime = (new Date(time)).getTime();
var gapTime = endTime-startTime;
var minutes = Math.floor(gapTime/(1000*60));
/*
8201分
1h = 60m
1t = 60*24
*/
// console.log(minutes)
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>