Skip to content

Instantly share code, notes, and snippets.

@maicong
Created March 1, 2018 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maicong/b1ab1c4ded2afaee05e31f63e4ec03e0 to your computer and use it in GitHub Desktop.
Save maicong/b1ab1c4ded2afaee05e31f63e4ec03e0 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>时间如白驹过隙</title>
</head>
<body>
<div id="timeout"></div>
<script>
// 转换时间
function timeout (second) {
const oneHour = 60 * 60
const oneDay = oneHour * 24
const oneYear = oneDay * 365
return [
parseInt(second / oneYear) + '年',
parseInt(second % oneYear / oneDay) + '天',
parseInt(second % oneDay / oneHour) + '小时',
parseInt(second % oneHour / 60) + '分',
parseInt(second % 60) + '秒'
]
.join('')
.replace(/\b(\d)\b/g, '0$1')
.replace(/\b(\d+)\b/g, ' $1 ')
.replace(/(\s00\s([^\s]+))?/g, '')
}
// 自执行函数
(function lost () {
const last = (new Date('2017-05-31T23:18:00')).getTime()
const now = (new Date()).getTime()
document.getElementById('timeout').innerText = timeout((now - last) / 1000)
setTimeout(() => lost(), 1000)
})()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment