获取时间戳
// 获得Date总的毫秒数(时间戳) 是距离1970年1月1号过了多少毫秒数
var date = new Date();
//方法1
console.log(date.valueOf());
// 方法2
console.log(date.getTime());
// 方法3
var date1 = +new Date();
console.log(date1);
//方法4 H5新增的
console.log(Date.now());
随机点名
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// console.log(getRandom(1, 10));
//随机点名
var arr = ["张三", "李四", "王五", "李白"];
console.log(arr[getRandom(0, arr.length - 1)]);
猜数字小游戏
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var random = getRandom(1, 10);
var i = 1;
while (i < 11) {
var num = prompt("你来猜? 输入1-10之间的一个数字");
i++;
if (num > random) {
alert("你猜大了");
} else if (num < random) {
alert("你猜小了");
} else {
alert("你好帅哦,猜对了");
break;
}
}
alert("好笨哦都给你十次机会啦还没猜到");
倒计时
function countDown(time) {
var nowTime = +new Date(); //当天的时间总毫秒数
var inputTime = +new Date(time); // 用户输入的总毫秒数
var times = (inputTime - nowTime) / 1000; // 剩余时间的总秒数
var d = parseInt(times / 60 / 60 / 24); //天
d = d < 10 ? "0" + d : d;
var h = parseInt((times / 60 / 60) % 24); // 时
h = h < 10 ? "0" + h : h;
var m = parseInt((times / 60) % 60); // 分
m = m < 10 ? "0" + m : m;
var s = parseInt(times % 60); //秒
s = s < 10 ? "0" + s : s;
return d + "天" + h + "时" + m + "分" + s + "秒";
}
console.log(countDown("2022-8-1 18:00:00"));
var date = new Date();
console.log(date);