func.js 946 B

1234567891011121314151617181920212223242526272829303132
  1. /******************
  2. *通用js函数
  3. *
  4. *******************/
  5. 'use strict';
  6. Date.prototype.Format = function(fmt) {
  7. var o = {
  8. "M+" : this.getMonth() + 1, // 月份
  9. "d+" : this.getDate(), // 日
  10. "h+" : this.getHours(), // 小时
  11. "m+" : this.getMinutes(), // 分
  12. "s+" : this.getSeconds(), // 秒
  13. "q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
  14. "S" : this.getMilliseconds()
  15. // 毫秒
  16. };
  17. if (/(y+)/.test(fmt))
  18. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  19. for ( var k in o)
  20. if (new RegExp("(" + k + ")").test(fmt))
  21. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  22. return fmt;
  23. };
  24. Date.prototype.toJSON = function() {
  25. return this.Format('yyyy-MM-dd hh:mm:ss');
  26. };
  27. String.prototype.toDate = function() {
  28. if (date == undefined || date.length != 19)
  29. return undefined;
  30. return new Date(Date.parse(date.replace(/-/g, "/")));
  31. };