Write the public method in the project src/utils/tool.js (custom js file):
import moment from 'moment-timezone' //引入moment
/**
* 格式化秒数为 UTC+时区时间格式
* */
//time为毫秒级时间戳,timezone为国际时区
export function timeFilter(time, timezone) {
if (!time) return '--'
time = Number(time); // 保证为number类型
timezone = timezone || 'Etc/UTC'
let timeFoot = moment(time).tz(timezone).format('Z')
let timeHead = moment(time).format('YYYY/MM/DD HH:mm:ss');
return time ? timeHead + ' UTC' + timeFoot : '--'
}
Use this method on the page:
<script>
import {
timeFilter } from '@/utils/tool' //引入我们写的timeFilter 方法
export default {
methods: {
getNowTime() {
//传入我们需要转换的updateTimeStamp和timeZone
//格式为 updateTimeStamp = 1683703286504
//格式为 timeZone = PRC
//结果为 nowTime = 2023/05/10 15:21:26 UTC+08:00
this.nowTime = timeFilter(this.updateTimeStamp, this.timeZone);
},
}
}
</script>