前端时间格式化
前端时间格式化
-
时间函数封装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28fmtDate(time = new Date()) {
Date.prototype.Format = function (fmt) {
var o = {
'M+': this.getMonth() + 1, // 月份
'd+': this.getDate(), // 日
'H+': this.getHours(), // 小时
'm+': this.getMinutes(), // 分
's+': this.getSeconds(), // 秒
'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
S: this.getMilliseconds(), //毫秒
}
if (/(y+)/.test(fmt))
fmt = fmt.replace(
RegExp.$1,
(this.getFullYear() + '').substr(4 - RegExp.$1.length)
)
for (var k in o)
if (new RegExp('(' + k + ')').test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1
? o[k]
: ('00' + o[k]).substr(('' + o[k]).length)
)
return fmt
}
return time.Format('yyyy-MM-dd HH:mm:ss')
}, -
使用
1
2
3
4
5
6
7
8
9
10
11
12// 默认 40 分钟的倒计时
laterTime() {
// 40分钟后的时间
var fmtTime = this.fmtDate()
var time = new Date(fmtTime.replace('-', '/'))
console.log('time:' + time)
var min = 40
time.setMinutes(time.getMinutes() + min, time.getSeconds(), 0)
console.log('time:' + time)
// 40分钟后的时间: console.log('164:' + this.fmtDate(time))
sessionStorage.setItem('inputTime', this.fmtDate(time))
}, -
倒计时实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21// 倒计时实现
getTime(date = '2022-12-20 22:10:00') {
date = sessionStorage.getItem('inputTime')
// 返回的是用户输入时间总毫秒数
var inputDate = +new Date(date)
// 返回当前时间的总毫秒数
var nowDate = +new Date()
// times 剩余时间总的秒数
var times = (inputDate - nowDate) / 1000
var h = parseInt((times / 60 / 60) % 24)
h = h < 10 ? '0' + h : h
this.hour = h
console.log('h: ' + this.hour)
var m = parseInt((times / 60) % 60)
m = m < 10 ? '0' + m : m
this.minute = m
var s = parseInt(times % 60)
s = s < 10 ? '0' + s : s
this.second = s
},
}, -
整体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253<!--待优化 -->
<template>
<div class="home">
<el-card class="box-card">
<el-tooltip
class="item"
effect="dark"
content="Click to enter custom time"
placement="top"
>
<h1 @click="inputTime" class="inputTime">Time Plan</h1>
</el-tooltip>
<p class="btn">
<el-button @click="study">study-time</el-button> /
<el-button type="info" @click="game"> game-time</el-button>
</p>
<h3 class="showTime">
<span>{{ hour }}</span> H <span> {{ minute }}</span> M
<span>{{ second }}</span> S
</h3>
</el-card>
<el-timeline :class="{ active: isShow, game: isGame }" ref="timeLineRef">
<el-timeline-item
v-for="(activity, index) in activities"
:key="index"
:icon="activity.icon"
:type="activity.type"
:color="activity.color"
:size="activity.size"
:timestamp="activity.timestamp"
>
{{ activity.content }}
</el-timeline-item>
</el-timeline>
<el-dialog
title="Time Management"
:visible.sync="dialogVisible"
width="30%"
:before-close="handleClose"
>
<div>
please enter time: <el-input type="text" v-model="inputDate" />
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">Cancel</el-button>
<el-button type="primary" @click="saveTime">Submit</el-button>
</span>
</el-dialog>
<el-card class="box-card dwn" :class="{ active: isShow }">
<router-view />
</el-card>
</div>
</template>
<script>
var timer = null
var sessionTime =
sessionStorage.getItem('inputTime') == null
? sessionStorage.setItem('inputTime', '2022-12-20 22:10:00')
: sessionStorage.getItem('inputTime')
console.log(sessionTime)
var clearId = null
export default {
data() {
return {
isShow: true, // display: none
dialogVisible: false,
hour: '00',
minute: '00',
second: '00',
dialogVisible: false,
inputDate: sessionTime,
afterDate: '',
}
},
created() {
var date = sessionStorage.getItem('inputTime')
this.getTime()
clearId = setInterval(() => {
this.getTime()
}, 1000)
},
methods: {
fmtDate(time = new Date()) {
Date.prototype.Format = function (fmt) {
var o = {
'M+': this.getMonth() + 1, //月份
'd+': this.getDate(), //日
'H+': this.getHours(), //小时
'm+': this.getMinutes(), //分
's+': this.getSeconds(), //秒
'q+': Math.floor((this.getMonth() + 3) / 3), //季度
S: this.getMilliseconds(), //毫秒
}
if (/(y+)/.test(fmt))
fmt = fmt.replace(
RegExp.$1,
(this.getFullYear() + '').substr(4 - RegExp.$1.length)
)
for (var k in o)
if (new RegExp('(' + k + ')').test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1
? o[k]
: ('00' + o[k]).substr(('' + o[k]).length)
)
return fmt
}
return time.Format('yyyy-MM-dd HH:mm:ss')
},
// 默认 40 分钟
laterTime() {
// 40分钟后的时间
var fmtTime = this.fmtDate()
var time = new Date(fmtTime.replace('-', '/'))
console.log('time:' + time)
var min = 40
time.setMinutes(time.getMinutes() + min, time.getSeconds(), 0)
console.log('time:' + time)
// 40分钟后的时间: console.log('164:' + this.fmtDate(time))
sessionStorage.setItem('inputTime', this.fmtDate(time))
},
study() {
this.isShow = !this.isShow
if (!this.isShow) {
this.$message.success(
'The default learning time is 40 minutes, you can enter a custom time'
)
this.laterTime()
this.getTime()
} else {
clearInterval(clearId)
this.$message.info('Stop the timer')
}
},
game() {
console.log('game....')
this.isGame = !this.isGame
if (this.isGame) {
this.$message.success(
'The default game is 40 minutes, you can enter a custom time...........'
)
this.isShow = true
this.isGame = true
window.location.reload()
this.laterTime()
this.getTime()
// 关闭显示 timeline
} else {
this.isShow = true
clearInterval(clearId)
this.$message.info('Stop the timer')
}
},
inputTime() {
this.dialogVisible = true
},
// 倒计时实现
getTime(date = '2022-12-20 22:10:00') {
date = sessionStorage.getItem('inputTime')
// 返回的是用户输入时间总毫秒数
var inputDate = +new Date(date)
// 返回当前时间的总毫秒数
var nowDate = +new Date()
// times 剩余时间总的秒数
var times = (inputDate - nowDate) / 1000
var h = parseInt((times / 60 / 60) % 24)
h = h < 10 ? '0' + h : h
this.hour = h
console.log('h: ' + this.hour)
var m = parseInt((times / 60) % 60)
m = m < 10 ? '0' + m : m
this.minute = m
var s = parseInt(times % 60)
s = s < 10 ? '0' + s : s
this.second = s
},
},
}
</script>
<style>
.home {
width: 375px;
margin: 100px auto;
display: flex;
justify-content: center;
text-align: center;
transform: translate(-10px);
}
.text {
font-size: 12px;
}
.item {
/* padding: 18px 0; */
}
.btn {
margin-top: 13px;
}
.el-button {
padding: 6px 4px ;
}
.box-card {
width: 330px ;
margin: -10px auto;
}
.el-timeline {
margin: 30px ;
}
.inputTime {
cursor: pointer;
}
.inputTime:hover {
color: orange;
transition: cubic-bezier(0.075, 0.82, 0.165, 1) 0.2s;
}
.showTime {
margin-top: 40px;
}
h3 span {
display: inline-block;
border: 1px solid rgba(0, 0, 0, 0.08);
padding: 5px;
border-radius: 5px;
}
h3 span:hover {
background-color: skyblue;
transition: all 0.2s ease-out;
}
.el-timeline {
margin-left: 10px;
}
.game,
.active {
display: none;
}
.el-card {
margin-left: 20px;
}
.dwn {
height: 70px;
}
.el-dialog {
position: fixed;
width: 250px ;
margin: 60px;
}
</style> -
渲染效果
渲染 自定义时间控制
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 coder-itl!
评论