util.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. Date.prototype.format = function(formatStr) {
  2. var str = formatStr;
  3. var Week = ['日', '一', '二', '三', '四', '五', '六'];
  4. str = str.replace(/yyyy|YYYY/, this.getFullYear());
  5. str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
  6. str = str.replace(/MM/, this.getMonth() > 8 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
  7. str = str.replace(/M/g, this.getMonth() + 1);
  8. str = str.replace(/w|W/g, Week[this.getDay()]);
  9. str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
  10. str = str.replace(/d|D/g, this.getDate());
  11. str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
  12. str = str.replace(/h|H/g, this.getHours());
  13. str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
  14. str = str.replace(/m/g, this.getMinutes());
  15. str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
  16. str = str.replace(/s|S/g, this.getSeconds());
  17. return str;
  18. }
  19. function isMobilePhone(n) {
  20. var reg = /^1[34578]\d{9}$/
  21. return reg.test(n)
  22. }
  23. function isTelPhone(n) {
  24. var reg = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/
  25. return reg.test(n)
  26. }
  27. function getDateFromNow(days) {
  28. if(!days) {
  29. return new Date().format("yyyy-MM-dd")
  30. }
  31. var now = new Date().getTime()
  32. var diff = 24 * 3600 * 1000 * days
  33. return new Date(now + diff).format("yyyy-MM-dd")
  34. }
  35. function GetRequest() {
  36. var url = location.search; //获取url中"?"符后的字串
  37. var theRequest = new Object();
  38. if(url.indexOf("?") != -1) {
  39. var str = url.substr(1);
  40. strs = str.split("&");
  41. for(var i = 0; i < strs.length; i++) {
  42. theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  43. }
  44. }
  45. return theRequest;
  46. }
  47. //判断输入字符串是否为空或全是空格
  48. function isNull(str) {
  49. if(str == null) return true;
  50. if(str == "") return true;
  51. var regu = "^[ ]+$";
  52. var re = new RegExp(regu);
  53. var bb = re.test(str);
  54. return re.test(str);
  55. }
  56. /**
  57. * 获取本周、本季度、本月、上月的开端日期、停止日期
  58. */
  59. var now = new Date(); //当前日期
  60. var nowDayOfWeek = now.getDay(); //今天本周的第几天
  61. var nowDay = now.getDate(); //当前日
  62. var nowMonth = now.getMonth(); //当前月
  63. var nowYear = now.getYear(); //当前年
  64. nowYear += (nowYear < 2000) ? 1900 : 0; //
  65. var lastMonthDate = new Date(); //上月日期
  66. lastMonthDate.setDate(1);
  67. lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
  68. var lastYear = lastMonthDate.getYear();
  69. var lastMonth = lastMonthDate.getMonth();
  70. //格局化日期:yyyy-MM-dd
  71. function formatDate(date) {
  72. var myyear = date.getFullYear();
  73. var mymonth = date.getMonth() + 1;
  74. var myweekday = date.getDate();
  75. if(mymonth < 10) {
  76. mymonth = "0" + mymonth;
  77. }
  78. if(myweekday < 10) {
  79. myweekday = "0" + myweekday;
  80. }
  81. return(myyear + "-" + mymonth + "-" + myweekday);
  82. }
  83. //获得某月的天数
  84. function getMonthDays(myMonth) {
  85. var monthStartDate = new Date(nowYear, myMonth, 1);
  86. var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  87. var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  88. return days;
  89. }
  90. //获得本季度的开端月份
  91. function getQuarterStartMonth() {
  92. var quarterStartMonth = 0;
  93. if(nowMonth < 3) {
  94. quarterStartMonth = 0;
  95. }
  96. if(2 < nowMonth && nowMonth < 6) {
  97. quarterStartMonth = 3;
  98. }
  99. if(5 < nowMonth && nowMonth < 9) {
  100. quarterStartMonth = 6;
  101. }
  102. if(nowMonth > 8) {
  103. quarterStartMonth = 9;
  104. }
  105. return quarterStartMonth;
  106. }
  107. //获得本周的开端日期
  108. function getWeekStartDate() {
  109. var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  110. return formatDate(weekStartDate);
  111. }
  112. //获得本周的停止日期
  113. function getWeekEndDate() {
  114. var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
  115. return formatDate(weekEndDate);
  116. }
  117. //获得本月的开端日期
  118. function getMonthStartDate() {
  119. var monthStartDate = new Date(nowYear, nowMonth, 1);
  120. return formatDate(monthStartDate);
  121. }
  122. //获得本月的停止日期
  123. function getMonthEndDate() {
  124. var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  125. return formatDate(monthEndDate);
  126. }
  127. //获得上月开端时候
  128. function getLastMonthStartDate() {
  129. var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
  130. return formatDate(lastMonthStartDate);
  131. }
  132. //获得上月停止时候
  133. function getLastMonthEndDate() {
  134. var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
  135. return formatDate(lastMonthEndDate);
  136. }
  137. //获得本季度的开端日期
  138. function getQuarterStartDate() {
  139. var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
  140. return formatDate(quarterStartDate);
  141. }
  142. //或的本季度的停止日期
  143. function getQuarterEndDate() {
  144. var quarterEndMonth = getQuarterStartMonth() + 2;
  145. var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
  146. return formatDate(quarterStartDate);
  147. }
  148. //获得本年的开始日期
  149. function getYearStartDate() {
  150. //获得当前年份4位年
  151. var currentYear=new Date().getFullYear();
  152. //本年第一天
  153. var currentYearFirstDate=new Date(currentYear,0,1);
  154. return formatDate(currentYearFirstDate);
  155. }
  156. //获得本年的结束日期
  157. function getYearEndDate() {
  158. //获得当前年份4位年
  159. var currentYear=new Date().getFullYear();
  160. //本年最后
  161. var currentYearLastDate=new Date(currentYear,11,31);
  162. return formatDate(currentYearLastDate);
  163. }