Home >Web Front-end >Vue.js >How to convert timestamp to time in vue.js
Vue.js method to convert timestamps into time: first install the moment plug-in; then introduce the moment plug-in in the [main.js] file; finally, directly reference dateFormat to convert the timestamp.
The operating environment of this tutorial: windows10 system, vue2.9, this article is applicable to all brands of computers.
Recommended: "vue.js Tutorial"
Time stamp is OK Understood as a way of filtering, date format conversion is often encountered in projects. Today I will summarize the pitfalls I have encountered in the project and the solutions.
cmd
Execute npm install moment --save
or yarn add moment
command to install the moment plug-inVue.filter('dateFormat', function(originVal) { const dt = new Date(originVal) const y = dt.getFullYear() const m = (dt.getMonth() + 1 + '').padStart(2, '0') const d = (dt.getDate() + '').padStart(2, '0') const hh = (dt.getHours() + '').padStart(2, '0') const mm = (dt.getMinutes() + '').padStart(2, '0') const ss = (dt.getSeconds() + '').padStart(2, '0') //时间格式年月日、时分秒 return `${y}-${m}-${d} ${hh}:${mm}:${ss}`})
HTML:
Js:
timestampToTime (time) {// How to convert timestamp to time in vue.js为10位需*1000,How to convert timestamp to time in vue.js为13位的话不需乘1000 var date = new Date(time * 1000) let y = date.getFullYear() let MM = date.getMonth() + 1 MM = MM < 10 ? ('0' + MM) : MM let d = date.getDate() d = d < 10 ? ('0' + d) : d let h = date.getHours() h = h < 10 ? ('0' + h) : h let m = date.getMinutes() m = m < 10 ? ('0' + m) : m let s = date.getSeconds() s = s < 10 ? ('0' + s) : s return y + '年' + MM + '月' + d + '日'}
Related free learning recommendations: JavaScript(video)
The above is the detailed content of How to convert timestamp to time in vue.js. For more information, please follow other related articles on the PHP Chinese website!