// 获取标准时间var date =newDate(0)
console.log(date)// 不加0是当前时间,加0返回的是标准时间// 有了标准时间可以设置获取时间戳,时间戳也就是把时间转化为毫秒数// 第一种方法:date.getTime()var date =newDate()
console.log(date.getTime())// 可以获取当前时间,再把当前时间转换为时间戳 var date =newDate('2022-10-1 00:00:00')
console.log(date.getTime())// 可以自定义时间,再把自定义时间转换为时间戳 // 第二种方法:Date.parse('时间')var date =newDate()
console.log(Date.parse('2022-10-1 00:00:00'))// 第三种:使用+号var date =+newDate()
console.log(date)
5. 时间戳转为天时分秒
const inp1 = document.getElementById("inp1")const btn = document.getElementById("btn")const show = document.getElementById("show")
btn.onclick=function(){let n =Number(inp1.value)// 先求天数(乘100后除100,可以解决精度丢失问题)let day =parseInt(parseInt(n *100/(24*60*60*1000))/100)// 求小时let hour =parseInt(parseInt(n %(24*60*60*1000)*100/60/60/1000)/100)// 分钟数let min =parseInt(n %(24*60*60*60*1000)%(60*60*1000)/60/1000)// 秒数let center =parseInt(n %(24*60*60*60*1000)%(60*60*1000)%(60*1000)/1000)// 毫秒数let mix =parseInt(n %(24*60*60*60*1000)%(60*60*1000)%(60*1000)%1000)// 打印
show.innerHTML =`${day}天${hour}小时${min}分钟${center}秒${mix}毫秒`}