時間戳處理

處理後臺返回的時間戳代碼:

 1 Date.prototype.Format = function (fmt) { /* author: meizz */
 2     var o = {
 3         "M+": this.getMonth() + 1, /* 月份 */
 4         "d+": this.getDate(), /**/
 5         "h+": this.getHours(), /* 小時 */
 6         "m+": this.getMinutes(), /**/
 7         "s+": this.getSeconds(), /**/
 8         "q+": Math.floor((this.getMonth() + 3) / 3), /* 季度 */
 9         "S": this.getMilliseconds() /* 毫秒 */
10     };
11     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
12     for (var k in o)
13         if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
14     return fmt;
15 };

調用方法:

1 new Date(str).Format("yyyy-MM-dd hh:mm");

  說明:Format 裏面傳的字符串能夠是其餘的格式 如:「yyyy年mm月dd日 hh時:mm分」。
  或其餘的格式(‘yyyy/mm/dd hh:mm’)等this

相關文章
相關標籤/搜索