需求:项目开发中,后端返回的日期以yyyy-MM-dd格式显示。

前端处理代码:
new Date().format('yyyy-MM-dd')
但报错

查找相关资料,原生js和jquery都不能直接用new Date().format(‘yyyy-MM-dd’)。
解决方法一:
js引入date.format.js 下载地址
<script src="date.format.js"></script>
解决方法二:
Date.prototype.format = function(format)
{var o = {"M+" : this.getMonth()+1, //month"d+" : this.getDate(), //day"h+" : this.getHours(), //hour"m+" : this.getMinutes(), //minute"s+" : this.getSeconds(), //second"q+" : Math.floor((this.getMonth()+3)/3), //quarter"S" : this.getMilliseconds() //millisecond}if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));for(var k in o)if(new RegExp("("+ k +")").test(format))format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] :("00"+ o[k]).substr((""+ o[k]).length));return format;
}
var date1 = new Date().format('yyyy-MM-dd');
console.log(date1);
春至花盛开,双影成三人。















