<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 时间戳10位:timestamp * 1000
// 时间戳13位:timestamp(实际 用这个就可以)
// 时间戳转日期
function timestampToDate(timestamp){
var date=new Date(timestamp);
var Y=date.getFullYear();
var M=date.getMonth()+1;
var D=date.getDate();
var h=date.getHours();
var m=date.getMinutes();
var s=date.getSeconds()>=10?date.getSeconds() : "0" + date.getSeconds();
return Y+"-"+M+"-"+D+" "+h+":"+m+":"+s
}
var currentTime=timestampToDate((new Date().getTime()))
console.log(currentTime);
// 日期转时间戳
function dateToTimestamp(date){
var date=new Date(date);
var timestamp=date.getTime();
return timestamp;
}
var timestamp=dateToTimestamp(currentTime);
console.log(timestamp);
</script>
</body>
</html>
<script>
//生成可控长度的随机数
function getID(len){
//引入时间戳:时间戳不可重复
var random=Math.random().toString().substr(2,len)+Date.now().toString(36);
return random;
}
var random=getID(6);
console.log(random);
</script>