util.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  9. }
  10. const getWeekByDate = dates => {
  11. let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  12. let date = new Date(dates);
  13. date.setDate(date.getDate());
  14. let day = date.getDay();
  15. return show_day[day];
  16. }
  17. const formatNumber = n => {
  18. n = n.toString()
  19. return n[1] ? n : `0${n}`
  20. }
  21. function formatDate(date){
  22. var year = date.getFullYear();
  23. var month = date.getMonth() + 1;
  24. var day = date.getDate();
  25. if(month<10)
  26. month="0"+month;
  27. if(day<10)
  28. day="0"+day;
  29. return [year, month, day].map(formatNumber).join('-') ;
  30. }
  31. //时间戳转日期,falg:true表示只要年月日,part: year month date
  32. function toDate(number,flag,part) {
  33. var n = number;
  34. var date = new Date(parseInt(n) * 1000);
  35. var y = date.getFullYear();
  36. var m = date.getMonth() + 1;
  37. m = m < 10 ? ('0' + m) : m;
  38. var d = date.getDate();
  39. d = d < 10 ? ('0' + d) : d;
  40. var h = date.getHours();
  41. h = h < 10 ? ('0' + h) : h;
  42. var minute = date.getMinutes();
  43. var second = date.getSeconds();
  44. minute = minute < 10 ? ('0' + minute) : minute;
  45. second = second < 10 ? ('0' + second) : second;
  46. if(flag){
  47. if(part == "year"){
  48. return y;
  49. }else if(part == "month"){
  50. return m;
  51. }else if(part == "date"){
  52. return n;
  53. }
  54. return y + '-' + m + '-' + d;
  55. }
  56. return y + '-' + m + '-' + d + ' ' + h + ':' + minute+':' + second;
  57. }
  58. //当前日期加减天数,falg:true表示只要年月日
  59. function mathChangeDate(date,method,days,flag){
  60. //method:'+' || '-'
  61. //ios不解析带'-'的日期格式,要转成'/',不然Nan,切记
  62. var dateVal = date.replace(/-/g, '/');
  63. var timestamp = Date.parse(dateVal);
  64. if(method == '+'){
  65. timestamp = timestamp / 1000 + 24 * 60 * 60 * days;
  66. } else if (method == '-'){
  67. timestamp = timestamp / 1000 - 24 * 60 * 60 * days;
  68. }
  69. return toDate(timestamp,flag);
  70. }
  71. /**
  72. * 封封微信的的request
  73. */
  74. function request(url, data = {}, method = "GET") {
  75. return new Promise(function(resolve, reject) {
  76. wx.request({
  77. url: url,
  78. data: data,
  79. method: method,
  80. header: {
  81. 'Content-Type': 'application/json',
  82. 'X-JZ-Token': wx.getStorageSync('token')
  83. },
  84. success: function(res) {
  85. if (res.statusCode == 200) {
  86. if (res.data.errno == 501) {
  87. // 清除登录相关内容
  88. try {
  89. wx.removeStorageSync('userInfo');
  90. wx.removeStorageSync('token');
  91. } catch (e) {
  92. // Do something when catch error
  93. }
  94. // 切换到登录页面
  95. wx.navigateTo({
  96. url: '/pages/index/index'
  97. });
  98. } else {
  99. resolve(res.data);
  100. }
  101. } else {
  102. reject(res.errMsg);
  103. }
  104. },
  105. fail: function(err) {
  106. reject(err)
  107. }
  108. })
  109. });
  110. }
  111. function showErrorToast(msg) {
  112. wx.showToast({
  113. title: msg,
  114. image: '/static/images/icon_error.png'
  115. })
  116. }
  117. function formatweek(date){
  118. let week=date.getDay();
  119. let weekArr='';
  120. switch (week){
  121. case 1:
  122. weekArr='周一';
  123. break;
  124. case 2:
  125. weekArr='周二';
  126. break;
  127. case 3:
  128. weekArr='周三';
  129. break;
  130. case 4:
  131. weekArr='周四';
  132. break;
  133. case 5:
  134. weekArr='周五';
  135. break;
  136. case 6:
  137. weekArr='周六';
  138. break;
  139. case 0:
  140. weekArr='周日';
  141. break;
  142. }
  143. return weekArr;
  144. }
  145. function getDateList(date,interval,length){
  146. let dateList=[];
  147. let len=(length?length:7);
  148. for(let i=0;i<len;i++){
  149. let datestamp=new Date(date.getTime()+i*60*60*1000*24*interval);
  150. let dateItem={
  151. week:formatweek(datestamp),
  152. date:formetdate(datestamp),
  153. id:new Date(datestamp).getDay()
  154. };
  155. dateList.push(dateItem);
  156. }
  157. return dateList;
  158. }
  159. function dateDifference( end) {
  160. // //比较2个日期
  161. // console.log(start + ",," + end);
  162. // var stime = new Date(start).getTime();
  163. var stime = new Date().getTime();
  164. console.log(end.replace(/-/g, "/"));
  165. var etime = new Date(end.replace(/-/g, "/")).getTime();
  166. var usedTime = etime - stime; //两个时间戳相差的毫秒数
  167. var days = Math.floor(usedTime / (24 * 3600 * 1000));
  168. //计算出小时数
  169. var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  170. var hours = Math.floor(leave1 / (3600 * 1000));
  171. //计算相差分钟数
  172. var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  173. var minutes = Math.floor(leave2 / (60 * 1000));
  174. if (days == 0 && hours == 0) {
  175. return minutes + "分钟";
  176. }
  177. if (days == 0) {
  178. return hours + "时" + minutes + "分钟";
  179. }
  180. var time = days + "天" + hours + "时" + minutes + "分钟";
  181. // var time = days;
  182. return time;
  183. }
  184. function formetdate(date){
  185. var year = date.getFullYear();
  186. var month = date.getMonth() + 1;
  187. var day = date.getDate();
  188. return [year, month, day].map(formatNumber).join('-') ;
  189. }
  190. module.exports = {
  191. formatTime,
  192. formatDate,
  193. request,
  194. mathChangeDate,
  195. showErrorToast,
  196. getWeekByDate,
  197. formatweek,
  198. getDateList,
  199. dateDifference,
  200. formetdate
  201. }