func.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. };
  32. //数组是否包含某个元素
  33. Array.prototype.inArray = function(e){
  34. for(var i = 0; i< this.length; i++){
  35. if(this[i] == e)
  36. return true;
  37. }
  38. return false;
  39. };