dateFormat.js 821 B

123456789101112131415161718192021
  1. /**
  2. * Created by vian on 2017/3/30.
  3. */
  4. Date.prototype.format = function(fmt)
  5. {
  6. var o = {
  7. "M+" : this.getMonth()+1, //月份
  8. "d+" : this.getDate(), //日
  9. "h+" : this.getHours(), //小时
  10. "m+" : this.getMinutes(), //分
  11. "s+" : this.getSeconds(), //秒
  12. "q+" : Math.floor((this.getMonth()+3)/3), //季度
  13. "S" : this.getMilliseconds() //毫秒
  14. };
  15. if(/(y+)/.test(fmt))
  16. fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  17. for(var k in o)
  18. if(new RegExp("("+ k +")").test(fmt))
  19. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  20. return fmt;
  21. }