vue 日期时间过滤器
2023-09-14 09:06:33 时间
来自:https://blog.csdn.net/m0_37068028/article/details/72898154 侵删
来自:https://segmentfault.com/a/1190000010378259 侵删
第一种:
// template
{{date | formatDate}}
//script
import {formatDate} from '../../common/js/date'
filters: {
formatDate (time) {
let date = new Date(time) return formatDate(date, 'yyyy-MM-dd hh:mm')
}
}
//date.js
export function formatDate (date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + ''
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
}
}
return fmt
}
function padLeftZero (str) {
return ('00' + str).substr(str.length)
}
第二种:
npm install moment --save
main.js 中
import moment from 'moment/moment'
添加过滤器:
Vue.filter('moment', function (value, formatString) { formatString = formatString || 'YYYY-MM-DD HH:mm:ss'; // return moment(value).format(formatString); // value可以是普通日期 20170723 return moment.unix(value).format(formatString); // 这是时间戳转时间 }); /* eslint-disable no-new */ new Vue({ el: '#app', template: '<App/>', components: { App } })
页面中使用
<template> <div id="app"> <div> {{ datetime | moment }} </div> </div> </template> <script> export default { name: 'app', data () { return { datetime: '1500799859' } } } </script>
结果:
2017-07-23 16:50:59
相关文章
- 简单vue项目脚手架
- 一个很好用的 vue-picker组件
- JS框架_(Vue.js)带有星期日期的数字时钟
- 深入浅出Vue基于“依赖收集”的响应式原理(转)
- [Poi] Build a Vue App with Poi
- vue.js3: 项目打包后部署到网站子目录(vue@3.2.33)
- [Vue] Dynamic Vue.js Components with the component element
- ElementUI+VUE 在使用ElementUI日期选择器时大于当前月份的日期不可选,默认日期
- Vue——服务器端渲染SSR
- 098:vue+openlayers的set extent 和 fit extent功能(示例代码)
- 测试开发之Vue学习笔记-H5实例百度音乐
- Vue(三)双向绑定、监视函数、事件修饰符、双向绑定原理
- SpringBoot 和 Vue 解决页面日期时间控件问题