util.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 resetClientWidth() {
  20. var width = document.body.clientWidth
  21. if(width >= 1920) {
  22. $('html').css('font-size', width/10 + 'px')
  23. } else {
  24. $(document.body).css('overflow', 'auto')
  25. $('html').css('font-size', 192 + 'px')
  26. }
  27. }
  28. if(!window.noUse)resetClientWidth()
  29. function isMobilePhone(n) {
  30. var reg = /^1[34578]\d{9}$/
  31. return reg.test(n)
  32. }
  33. function isTelPhone(n) {
  34. var reg = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/
  35. return reg.test(n)
  36. }
  37. function getDateFromNow(days) {
  38. if(!days) {
  39. return new Date().format("yyyy-MM-dd")
  40. }
  41. var now = new Date().getTime()
  42. var diff = 24 * 3600 * 1000 * days
  43. return new Date(now + diff).format("yyyy-MM-dd")
  44. }
  45. function GetRequest() {
  46. var url = location.search; //获取url中"?"符后的字串
  47. var theRequest = new Object();
  48. if(url.indexOf("?") != -1) {
  49. var str = url.substr(1);
  50. strs = str.split("&");
  51. for(var i = 0; i < strs.length; i++) {
  52. theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  53. }
  54. }
  55. return theRequest;
  56. }
  57. //判断输入字符串是否为空或全是空格
  58. function isNull(str) {
  59. if(str == null) return true;
  60. if(str == "") return true;
  61. var regu = "^[ ]+$";
  62. var re = new RegExp(regu);
  63. var bb = re.test(str);
  64. return re.test(str);
  65. }
  66. /**
  67. * 获取本周、本季度、本月、上月的开端日期、停止日期
  68. */
  69. var now = new Date(); //当前日期
  70. var nowDayOfWeek = now.getDay(); //今天本周的第几天
  71. var nowDay = now.getDate(); //当前日
  72. var nowMonth = now.getMonth(); //当前月
  73. var nowYear = now.getYear(); //当前年
  74. nowYear += (nowYear < 2000) ? 1900 : 0; //
  75. //nowYear = nowYear-1;//由于2018年没数据,故查询2017年数据(测试)
  76. var lastMonthDate = new Date(); //上月日期
  77. lastMonthDate.setDate(1);
  78. lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
  79. var lastYear = lastMonthDate.getYear();
  80. var lastMonth = lastMonthDate.getMonth();
  81. //格局化日期:yyyy-MM-dd
  82. function formatDate(date) {
  83. var myyear = date.getFullYear();
  84. var mymonth = date.getMonth() + 1;
  85. var myweekday = date.getDate();
  86. if(mymonth < 10) {
  87. mymonth = "0" + mymonth;
  88. }
  89. if(myweekday < 10) {
  90. myweekday = "0" + myweekday;
  91. }
  92. return(myyear + "-" + mymonth + "-" + myweekday);
  93. }
  94. //获得某月的天数
  95. function getMonthDays(myMonth) {
  96. var monthStartDate = new Date(nowYear, myMonth, 1);
  97. var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  98. var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  99. return days;
  100. }
  101. //获得本季度的开端月份
  102. function getQuarterStartMonth() {
  103. var quarterStartMonth = 0;
  104. if(nowMonth < 3) {
  105. quarterStartMonth = 0;
  106. }
  107. if(2 < nowMonth && nowMonth < 6) {
  108. quarterStartMonth = 3;
  109. }
  110. if(5 < nowMonth && nowMonth < 9) {
  111. quarterStartMonth = 6;
  112. }
  113. if(nowMonth > 8) {
  114. quarterStartMonth = 9;
  115. }
  116. return quarterStartMonth;
  117. }
  118. //获得本周的开端日期
  119. function getWeekStartDate() {
  120. var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  121. return formatDate(weekStartDate);
  122. }
  123. //获得本周的停止日期
  124. function getWeekEndDate() {
  125. var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
  126. return formatDate(weekEndDate);
  127. }
  128. //获得本月的开端日期
  129. function getMonthStartDate() {
  130. var monthStartDate = new Date(nowYear, nowMonth, 1);
  131. return formatDate(monthStartDate);
  132. }
  133. //获得本月的停止日期
  134. function getMonthEndDate() {
  135. var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  136. return formatDate(monthEndDate);
  137. }
  138. //获得上月开端时候
  139. function getLastMonthStartDate() {
  140. var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
  141. return formatDate(lastMonthStartDate);
  142. }
  143. //获得上月停止时候
  144. function getLastMonthEndDate() {
  145. var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
  146. return formatDate(lastMonthEndDate);
  147. }
  148. //获得本季度的开端日期
  149. function getQuarterStartDate() {
  150. var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
  151. return formatDate(quarterStartDate);
  152. }
  153. //或的本季度的停止日期
  154. function getQuarterEndDate() {
  155. var quarterEndMonth = getQuarterStartMonth() + 2;
  156. var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
  157. return formatDate(quarterStartDate);
  158. }
  159. //获得本年的开始日期
  160. function getYearStartDate() {
  161. //本年第一天
  162. var currentYearFirstDate=new Date(nowYear,0,1);
  163. return formatDate(currentYearFirstDate);
  164. }
  165. //获得本年的结束日期
  166. function getYearEndDate() {
  167. //本年最后
  168. var currentYearLastDate=new Date(nowYear,11,31);
  169. return formatDate(currentYearLastDate);
  170. }
  171. //获得昨天日期
  172. function getYearEndDate() {
  173. return getYesterday(-1, '-');
  174. }
  175. //近半年开始时间
  176. function getHalfYearStartDate(){
  177. var date = new Date();
  178. date.setMonth(date.getMonth() - 6);
  179. var year = nowYear;
  180. var month = date.getMonth() + 1;
  181. month = (month < 10 ? "0" + month : month);
  182. return year.toString() + '-' + month.toString() + "-01";
  183. }
  184. //近半年结束时间
  185. function getHalfYearEndDate(){
  186. var date = new Date();
  187. var currentMonth = date.getMonth();
  188. var nextMonth = ++currentMonth;
  189. var nextMonthFirstDay = new Date(nowYear, nextMonth-1, 1);
  190. var oneDay = 1000 * 60 * 60 * 24;
  191. return(new Date(nextMonthFirstDay - oneDay)).format("yyyy-MM-dd");
  192. }
  193. //获取过去12个月时间,不包含本月
  194. function getLastYearMonthArray(){
  195. var dataArr = [],day,month;
  196. var data=new Date();
  197. var year=data.getFullYear()-1;
  198. data.setMonth(data.getMonth()+1)//获取到当前月份,设置月份
  199. for (var i = 0; i <=12; i++) {
  200. data.setMonth(data.getMonth()-1);//每次循环一次 月份值减1
  201. month = (data.getMonth()+1)<10?"0"+(data.getMonth()+1):data.getMonth()+1;
  202. if(i==12){//过去第12个月
  203. dataArr.push(data.getFullYear()+"-"+month+"-01")
  204. }else if(i==0){//本月
  205. var day = getDaysInMonth(data.getFullYear(),data.getMonth()+1);
  206. dataArr.push(data.getFullYear()+"-"+month+"-"+day)
  207. }else{
  208. dataArr.push(data.getFullYear()+"-"+month)
  209. }
  210. }
  211. return dataArr;
  212. }
  213. //根据年月实现获得当月天数
  214. function getDaysInMonth(year,month){
  215. month = parseInt(month,10); //parseInt(number,type)这个函数后面如果不跟第2个参数来表示进制的话,默认是10进制。
  216. var temp = new Date(year,month,0);
  217. return temp.getDate();
  218. }
  219. //获取昨天日期
  220. function getYesterday(num, str){
  221. var today = new Date();
  222. var nowTime = today.getTime();
  223. var ms = 24*3600*1000*num;
  224. today.setTime(parseInt(nowTime + ms));
  225. var oYear = nowYear;
  226. var oMoth = (today.getMonth() + 1).toString();
  227. if (oMoth.length <= 1) oMoth = '0' + oMoth;
  228. var oDay = today.getDate().toString();
  229. if (oDay.length <= 1) oDay = '0' + oDay;
  230. return oYear + str + oMoth + str + oDay;
  231. }
  232. //获取过去6个月的开始时间
  233. function getLastHalfMonthArray(dateStr){//格式:2018-07
  234. var dataArr = [],day,month,halfDate;
  235. var data=new Date();
  236. data.setFullYear(parseInt(dateStr.substring(0,4)));
  237. data.setMonth(parseInt(dateStr.substring(5,7)))//获取到当前月份,设置月份
  238. data.setMonth(data.getMonth()-6);//往前推半年,月份值减6
  239. month = (data.getMonth()+1)<10?"0"+(data.getMonth()+1):data.getMonth()+1;
  240. halfDate = data.getFullYear()+"-"+month+"-01"
  241. return halfDate;
  242. }
  243. //获取当前时间往前推3个季度
  244. function getThirdJiDate(year,month){
  245. year = Number(year);
  246. month = Number(month);
  247. var t = 11;
  248. if(month<=3){
  249. t = t - 3 + month;
  250. }else if(month>3 && month<=6){
  251. t = t - 6 + month;
  252. }else if(month>6 && month<=9){
  253. t = t - 9 + month;
  254. }else if(month>9 && month<=12){
  255. t = t - 12 + month;
  256. }
  257. if(t>=month){
  258. year = year - 1;
  259. month = 12 + month -t;
  260. }else{
  261. month = month - t;
  262. }
  263. var resDate;
  264. if(month<10){
  265. resDate = year +'-0'+month+'-01';
  266. }else{
  267. resDate = year +'-'+month+'-01';
  268. }
  269. console.log(resDate)
  270. return resDate;
  271. }
  272. //获取当前时间往前推4个季度
  273. function getFirthJiDate(year,month){
  274. year = Number(year);
  275. month = Number(month);
  276. var t = 14;
  277. if(month<=3){
  278. t = t - 3 + month;
  279. }else if(month>3 && month<=6){
  280. t = t - 6 + month;
  281. }else if(month>6 && month<=9){
  282. t = t - 9 + month;
  283. }else if(month>9 && month<=12){
  284. t = t - 12 + month;
  285. }
  286. if(t>=month){
  287. year = year - 1;
  288. month = 12 + month -t;
  289. }else{
  290. month = month - t;
  291. }
  292. var resDate;
  293. if(month<10){
  294. resDate = year +'-0'+month+'-01';
  295. }else{
  296. resDate = year +'-'+month+'-01';
  297. }
  298. console.log(resDate)
  299. return resDate;
  300. }
  301. Date.prototype.Format = function (fmt) { //author: meizz
  302. var o = {
  303. "M+": this.getMonth() + 1, //月份
  304. "d+": this.getDate(), //日
  305. "h+": this.getHours(), //小时
  306. "m+": this.getMinutes(), //分
  307. "s+": this.getSeconds(), //秒
  308. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  309. "S": this.getMilliseconds() //毫秒
  310. };
  311. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  312. for (var k in o)
  313. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) :
  314. (("00" + o[k]).substr(("" + o[k]).length)));
  315. return fmt;
  316. }
  317. function updateFontSize(){
  318. var width = document.body.clientWidth
  319. console.log(width)
  320. if(width >= 1920) {
  321. $('html').css('font-size', width/10 + 'px')
  322. } else {
  323. $(document.body).css('overflow', 'auto')
  324. $('html').css('font-size', width/7.7 + 'px')
  325. }
  326. }
  327. //获取地址栏参数
  328. function getUrlParms(name){
  329. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  330. var r = window.location.search.substr(1).match(reg);
  331. if(r!=null)
  332. return unescape(r[2]);
  333. return null;
  334. }