vue的时间过滤器

vue的时间过滤器

<template>
	<div id="app">
		<div>当前时间是: {{ dateTime }</div>
		<div>格式化后的时间是: {{ dateTime | dateFormat }}</div>
	</div>
</template>
<script>
	var vm = new Vue({
		el: '#app',
		data: {
			dateTime: new Date(),
			},
			// 定义一个局部的过滤器,格式化当前时间
			filters: {
				dateFormat: (dateTime) => {
					var now = new Date(dateTime)
					var y = now.getFullYear()
					var m = (now.getMonth() + 1).toString().padStart(2, '0')
					var d = now.getDate().toString().padStart(2, '0')
					var hh = now.getHours().toString().padStart(2, '0')
					var mm = now.getMinutes().toString().padStart(2, '0')
					var ss = now.getSeconds().toString().padStart(2, '0')
					// 过滤器中要有返回值
					return `${y}-${m}-${d} ${hh}:${mm}:${ss}`               
				}
			}
		})
</script>

你可能感兴趣的:(笔记)