Date = function (Date) { CompatDate.prototype = Date.prototype; CompatDate.JavaScriptDate = Date; CompatDate.UTC = Date.UTC; CompatDate.parse = Date.parse; CompatDate.now = function(){ new Date().getTime(); }; return CompatDate; function CompatDate() { if (arguments.length === 1) { if(!(arguments[0] instanceof Date && !isNaN(arguments[0]))){ let arg = arguments[0]; if (Object.prototype.toString.call(arg) === '[object String]' ) { if(arg.indexOf('GMT')>0){ return new CompatDate.JavaScriptDate(arg); }else { if(arg.length<=7 && arg.indexOf('-')>1) { arg+="-01"; // yyyy-MM 转成 yyyy-MM-01 } arguments[0] = arg.replace(/-/g, "/").replace(/T/g,' ').replace(/\.[\s\S]*/,''); } } } } let bind = Function.bind; let unbind = bind.bind(bind); return new (unbind(Date, null).apply(null, arguments)); } }(Date); Date.prototype.format = function(fmt){ if(!(this instanceof Date && !isNaN(this))){ return ''; } 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.toString(); }; function textDateFormat(input,fmt) { if(input){ var v; if(input.length<=7 && input.indexOf('-')>1){ //'yyyy-MM' v = new Date(Date.parse((input+"-01").replace(/-/g,"/"))); }else{ v = new Date(Date.parse(input.replace(/-/g,"/").replace(/T/g," "))); } return v.format(fmt); }else{ return input; } }
js日期加工处理
最新推荐文章于 2024-07-02 15:42:57 发布