1.新建一个comm.js文件


function formatTimeToStr(times, pattern) {
	var d = new Date(times).Format('yyyy-MM-dd hh:mm:ss')
	if (pattern) {
		d = new Date(times).Format(pattern)
	}
	return d.toLocaleString()
}

Date.prototype.Format = function (fmt) {
	var o = {
		'M+': this.getMonth() + 1, // 月份
		'd+': this.getDate(), // 日
		'h+': this.getHours(), // 小时
		'm+': this.getMinutes(), // 分
		's+': this.getSeconds(), // 秒
		'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
		'S': this.getMilliseconds() // 毫秒
	}
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
	}
	for (var k in o) {
		if (new RegExp('(' + k + ')').test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
		}
	}
	return fmt
}
function formatDates(time){
	if (time !== null && time !== '') {
		var date = new Date(time)
		return formatTimeToStr(date, 'yyyy-MM-dd hh:mm:ss')
	} else {
		return ''
	}
}
export default{formatDates}

如果只要年月日可以去掉 hh:mm:ss

2.在页面中引用

先引入

import formatDates from "../../static/js/comm.js"

方法中使用

formatDates.formatDates(aa) //aa是要转换的值

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐