当前位置:网站首页>【js组件】date日期显示。
【js组件】date日期显示。
2022-07-06 23:26:00 【KatyLight】

日期组件,会随着时间变化。实现代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
background-color: black;
color: white;
}
.KatyLightTimeBox{
color: #00ff95;
font-size: 2rem;
font-weight: bolder;
}
.KatyLightTimeBox>.time{
margin-left: 1rem;
}
</style>
<script>
class KatyLightTimeBox{
constructor(params) {
this.target=params.target;
let date=new Date();
let year=date.getFullYear();
let mouth=date.getMonth()+1;
let d=date.getDate();
let h=date.getHours();
let m=date.getMinutes();
let s=date.getSeconds();
this.timeSpan=this.target.querySelector('.time')
this.dateSpan=this.target.querySelector('.date')
this.data=[
year,mouth,d,h,m,s
]
this.dateSpan.innerText=this.dateFormat();
this.run()
}
run(){
setInterval(()=>{
let cc=false;
this.data[5]++;
if(this.data[5]>=60){
this.data[5]=0;
this.data[4]++
}
if(this.data[4]>=60){
this.data[4]=0;
this.data[3]++
}
if(this.data[3]>=24){
this.data[3]=0;
this.data[2]++
cc=true;
}
this.timeSpan.innerText=this.timeFormat();
if(!cc){
return;
}
let max=30;
if(this.data[1]<8){
if(this.data[1]%2===0){
if(this.data[1]===2){
if(this.data[0]%400===0||(this.data[0]%4===0&&this.data[0]%100!==0)){
max=29
}else{
max=28
}
}else{
max=30
}
}else{
max=31
}
}else{
if(this.data[1]%2===0){
max=31
}else{
max=30
}
}
if(this.data[2]>max){
this.data[2]=0;
this.data[1]++;
}
if(this.data[1]>12){
this.data[1]=1;
this.data[0]++;
}
this.dateSpan.innerText=this.dateFormat();
},1000)
}
dateFormat(){
let s=this.data[0].toString();
let m=this.data[1].toString();
if(m.length<2){
m="0"+m;
}
let h=this.data[2].toString();
if(h.length<2){
h="0"+h;
}
return h+"."+m+"."+s
}
timeFormat(){
let s=this.data[5].toString();
if(s.length<2){
s="0"+s;
}
let m=this.data[4].toString();
if(m.length<2){
m="0"+m;
}
let h=this.data[3].toString();
if(h.length<2){
h="0"+h;
}
return h+":"+m+":"+s
}
}
window.onload=()=>{
new KatyLightTimeBox({
target:document.querySelector('.KatyLightTimeBox')
})
}
</script>
</head>
<body>
<div class="KatyLightTimeBox"><span class="date">2012.12.21</span><span class="time">12:00:00</span></div>
</body>
</html>
边栏推荐
猜你喜欢

No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities

Longest palindrome substring (dynamic programming)

高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍

Harmonyos fourth training
![[opencv] image morphological operation opencv marks the positions of different connected domains](/img/c3/f437bad9432dedbbb14c8a62ba5180.png)
[opencv] image morphological operation opencv marks the positions of different connected domains

U++4 interface learning notes

pytest测试框架——数据驱动

《五》表格

漏电继电器LLJ-100FS

【opencv】图像形态学操作-opencv标记不同连通域的位置
随机推荐
Auto.js 获取手机所有app名字
做自媒体,有哪些免费下载视频剪辑素材的网站?
一个酷酷的“幽灵”控制台工具
ClickHouse(03)ClickHouse怎么安装和部署
LinkedBlockingQueue源码分析-初始化
腾讯云数据库公有云市场稳居TOP 2!
app内嵌h5---iphone软键盘遮挡输入文字
What changes will PMP certification bring?
DJ-ZBS2漏电继电器
Techniques d'utilisation de sublime
The founder has a debt of 1billion. Let's start the class. Is it about to "end the class"?
The sooner you understand the four rules of life, the more blessed you will be
App embedded H5 --- iPhone soft keyboard blocks input text
NiO related knowledge points (I)
Why is the salary of test and development so high?
2039: [Bluebridge cup 2022 preliminaries] Li Bai's enhanced version (dynamic planning)
最长公共子序列(LCS)(动态规划,递归)
ScheduledExecutorService定时器
Harmonyos fourth training
Torch optimizer small parsing