vue.js 抽取全局日期格式化的公用方法

3,529 阅读1分钟

Vue项目 main.js 为入口js文件,在main.js 文件加入以下的代码

在各个界面的日期类型的变量格式化,可以直接使用日期.Format("yyyy-MM-dd")等方式,例如:new Date().Format("yyyy-MM-dd hh:mm:ss");

/**
 * @param fmt
 * @returns {*}
 * @constructor 日期的格式化
 */
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;
}

其他博客:
docker安装18.03.0+rancher1.6.17 的容器虚拟化部署
vue.js+iview 实现全局加载的公用方法
欢迎留言评论学习