uniapp中怎么把后台返回0001-01-01T00:00:00Z转换成时间

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是要转换的值

猜你喜欢

转载自blog.csdn.net/stars______/article/details/130627072
00