<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 参数time为空格间隔的字符串时间
function formatTimeToChinese(time) {
time = time.split('')
'年月日时分秒'.split('').forEach(e => {
for (let i = 0; i < time.length; i++) {
let t = time[i]
if (t === ' ') {
time[i] = e
return
}
}
time.push(e)
})
return time.join('')
}
function diyTime(params) {
try {
let time = params.time
let speed = params.speed
let year = time.getFullYear()
let month = time.getMonth() + 1
let day = time.getMinutes()
let hour = time.getHours()
let minute = time.getMinutes()
let second = time.getSeconds()
// 间隔
let interval = parseInt(1 / (1 / speed))
let s = `${year} ${month} ${day} ${hour} ${minute} ${second}`.split(' ')
let t
console.log(s.join(' '));
function sum(a, b) {
try {
return Number(a) + Number(b)
} catch (error) {
console.warn(error);
}
}
function increase() {
t = setTimeout(() => {
// 进行时间计算
s[5] = sum(s[5], interval)
let remainder
let multiple
s.reverse().forEach((e, index) => {
switch (index) {
// 秒
case 0:
{
let t = s[index]
multiple = parseInt(t / 60)
remainder = t % 60
if (multiple >= 1) {
s[index + 1] = sum(s[index + 1], multiple)
s[index] = remainder
}
break;
}
// 分
case 1:
{
let t = s[index]
multiple = parseInt(t / 60)
remainder = t % 60
if (multiple >= 1) {
s[index + 1] = sum(s[index + 1], multiple)
s[index] = remainder
}
break;
}
// 时
case 2:
{
let t = s[index]
multiple = parseInt(t / 24)
remainder = t % 24
if (multiple >= 1) {
s[index + 1] = sum(s[index + 1], multiple)
s[index] = remainder
}
break;
}
// 日
case 3:
{
let t = s[index]
let fullMoon = '1 3 5 7 8 10 12'
let moon = '4 6 9 11'
if (fullMoon.indexOf(year) + 1 > 0) {
multiple = parseInt(t / 31)
remainder = t % 31
if (multiple > 1) {
s[index + 1] = sum(s[index + 1], multiple)
s[index] = remainder
}
} else if (moon.indexOf(year) + 1 > 0) {
multiple = parseInt(t / 30)
remainder = t % 30
if (multiple > 1) {
s[index + 1] = sum(s[index + 1], multiple)
s[index] = remainder
}
} else if (Number(year) === 2) {
multiple = parseInt(t / 28)
remainder = t % 28
if (multiple > 1) {
s[index + 1] = sum(s[index + 1], remainder)
s[index] = remainder
}
}
break;
}
// 月
case 4:
{
let t = s[index]
multiple = parseInt(t / 12)
remainder = t % 12
if (multiple > 1) {
s[index + 1] = sum(s[index + 1], remainder)
s[index] = remainder
}
break;
}
// 年
case 5:
{
break;
}
default:
break;
}
})
console.log(formatTimeToChinese(s.reverse().join(' ')));
clearTimeout(t)
increase()
}, 1000)
}
increase()
} catch (error) {
console.warn("参数错误", error);
}
}
diyTime({ speed: 1000, time: new Date() })
</script>
</body>
</html>
标签:index,multiple,自定义,sum,js,let,time,倍速,remainder
From: https://www.cnblogs.com/lambertlt/p/16774368.html