Javascript Date Format(fmt)
Date.prototype.Format = function(fmt) { var o = {//from w ww .j a va 2 s.co m "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; }
Date.prototype.format = function (fmt) { var o = {/* w ww . j a va2 s . c o m*/ "M+": this.getMonth() + 1, //Month "d+": this.getDate(), //Day "h+": this.getHours(), //Hour "m+": this.getMinutes(), //Minute "s+": this.getSeconds(), //Second "S": this.getMilliseconds() //Millisecond }; 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; };
/**//from ww w . j a va2 s . c o m * Created by Sin on 14-11-13. */ 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; };
// javascript file Date.DATETIME_FORMAT = "yyyy-MM-dd hh:mm:ss"; Date.DATE_FORMAT = "yyyy-MM-dd"; Date.prototype.format = function(fmt) { var o = {//w ww . j av a 2 s .c om y: this.getFullYear(), M: this.getMonth() + 1, d: this.getDate(), h: this.getHours(), m: this.getMinutes(), s: this.getSeconds() }; return fmt.replace(/(y+|M+|d+|h+|m+|s+)/g, function(e) { var key = e.charAt(0); if (e.length > 1) { o[key] = "0" + o[key]; } return (o[key].toString().slice(-(e.length > 2 ? e.length : 2))); }); }
Date.prototype.format = function (fmt) { var date = this; return fmt.replace( /\{([^}:]+)(?::(\d+))?\}/g,/*from w w w. ja v a2s.c o m*/ function (s, comp, pad) { var fn = date["get" + comp]; if (fn) { var v = (fn.call(date) + (/Month$/.test(comp) ? 1 : 0)).toString(); return pad && (pad = pad - v.length) ? new Array(pad + 1).join("0") + v : v; } else { return s; } }); };
Date.prototype.Format = function(fmt) { var k, o, str; o = {//ww w. j a v a 2 s . c o m "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 (k in o) { if (!(new RegExp("(" + k + ")").test(fmt))) { continue; } if (RegExp.$1.length === 1) { str = o[k]; } else { str = ("00" + o[k]).substr(("" + o[k]).length); } fmt = fmt.replace(RegExp.$1, str); } return fmt; }; module.exports = Date.prototype.Format;
// format the date into a specific way Date.prototype.Format = function (fmt) { var o = {/*from w w w . j a va 2s . co m*/ "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; } // get the formated current time exports.currentTime = function(){ return new Date().Format("yyyy-MM-dd HH:mm:ss"); } // format a date exports.format = function(date){ return date.Format("yyyy-MM-dd HH:mm:ss"); }