在開發(fā)時(shí),經(jīng)常遇到轉(zhuǎn)換時(shí)間戳的問(wèn)題,這里封裝了一個(gè)方法,方便使用。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-637551.html
1.封裝方法:src/utils/time.js
/*
* @Author: maxiaotiao
* @Description: 時(shí)間戳轉(zhuǎn)換
* @FilePath: src/utils/time.js
*/
class Time {
// 1645839048000 --> 2022-02-26 09:30:48
formatTime (current, isShow) {
if (!current) {
return
}
const date = new Date(current);
const y = date.getFullYear();
const m = (date.getMonth() + 1) < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1;
const d = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
const h = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours();
const mi = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
const s = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds();
if(isShow) {
return `${y}${m}${d}${h}${mi}${s}`
}
return `${y}-${m}-${d} ${h}:${mi}:${s}`;
}
}
export default new Time();
2.頁(yè)面使用:index.vue
<script>
import timeUtil from '@/utils/time';
methods: {
getTime(){
let showTime = timeUtil.formatTime(new Date(new Date()), false)
console.log('當(dāng)前時(shí)間1', new Date()); //Sat Feb 26 2022 09:38:58 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
console.log('當(dāng)前時(shí)間2', showTime); //2022-02-26 09:38:58
}
}
</script>
完成!
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-637551.html
到了這里,關(guān)于vue封裝-獲取當(dāng)前時(shí)間的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!