Skip to content

Instantly share code, notes, and snippets.

@littledian
Created February 1, 2019 08:58
Show Gist options
  • Save littledian/47894e0217bc110197e75a7b56f19c0b to your computer and use it in GitHub Desktop.
Save littledian/47894e0217bc110197e75a7b56f19c0b to your computer and use it in GitHub Desktop.
index.html
<html lang="zh">
<head title="倒计时"></head>
<style>
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: green;
flex-direction: column;
}
</style>
<body>
<script>
const el1 = document.createElement('h1')
document.body.appendChild(el1)
const el2 = document.createElement('h1')
document.body.appendChild(el2)
function format (milliseconds) {
if (milliseconds < 0) milliseconds = 0
const seconds = parseInt(milliseconds / 1000) % 60
const minutes = parseInt(milliseconds / 1000 / 60) % 60
const hours = parseInt(milliseconds / 1000 / 60 / 60) % 24
const day = parseInt(milliseconds / 1000 / 60 / 60 / 24)
return `${day}天${hours}小时${minutes}分${seconds}秒`
}
setInterval(() => {
const date = new Date()
const end1 = new Date(2019, 1, 1, 18)
const end2 = new Date(2019, 1, 11, 10)
const s1 = format(end1.getTime() - date.getTime())
const s2 = format(end2.getTime() - date.getTime())
el1.innerText = `离下班还有${s1}`
el2.innerText = `离上班还有${s2}`
}, 10)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment