util.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. function formatTime(date) {
  2. var year = date.getFullYear()
  3. var month = date.getMonth() + 1
  4. var day = date.getDate()
  5. var hour = date.getHours()
  6. var minute = date.getMinutes()
  7. var second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  9. }
  10. function Getdate(date) {
  11. var year = date.getFullYear()
  12. var month = date.getMonth() + 1
  13. var day = date.getDate()
  14. return [year, month, day].map(formatNumber).join('.')
  15. }
  16. function formatNumber(n) {
  17. n = n.toString()
  18. return n[1] ? n : '0' + n
  19. }
  20. module.exports = {
  21. Getdate:Getdate,
  22. formatTime: formatTime
  23. , showLoading: function (loading) {
  24. this.getCurrentPage().setData({
  25. show_loading: loading
  26. })
  27. }
  28. , showSuccess: function (title) {
  29. wx.showToast({
  30. title: title,
  31. icon: 'success',
  32. duration: 2000
  33. })
  34. }
  35. , showModal: function (title, content, cb) {
  36. wx.showModal({
  37. title: title,
  38. content: content == undefined ? '' : content,
  39. showCancel: false,
  40. success: function (res) {
  41. if (cb) {
  42. cb(res)
  43. }
  44. }
  45. })
  46. }
  47. // 获取当前页面
  48. ,getCurrentPage:function(){
  49. var arr = getCurrentPages();
  50. return arr[arr.length - 1]
  51. }
  52. /**获取今天零点的时间戳*/
  53. , getToday: function () {
  54. var today = new Date();
  55. today.setHours(0);
  56. today.setMinutes(0);
  57. today.setSeconds(0);
  58. today.setMilliseconds(0);
  59. return Math.floor(today.getTime() / 1000);
  60. },
  61. /**获取这个月的第一天时间戳*/
  62. getThisMonth: function () {
  63. var today = new Date();
  64. today.setDate(0);
  65. today.setHours(0);
  66. today.setMinutes(0);
  67. today.setSeconds(0);
  68. today.setMilliseconds(0);
  69. return Math.floor(today.getTime() / 1000);
  70. },
  71. /**获取实时时间戳*/
  72. getNowTimestamp: function () {
  73. return Math.floor(new Date().getTime() / 1000);
  74. },
  75. /**时间戳转日期格式*/
  76. timestamp2date: function format(timestamp) {
  77. function add0(m) { return m < 10 ? '0' + m : m }
  78. var time = new Date(timestamp);
  79. var y = time.getFullYear();
  80. var m = time.getMonth() + 1;
  81. var d = time.getDate();
  82. var h = time.getHours();
  83. var mm = time.getMinutes();
  84. var s = time.getSeconds();
  85. return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
  86. }
  87. //获取当前分钟数的时间戳
  88. , getNowMinute: function () {
  89. var today = new Date();
  90. today.setSeconds(0);
  91. today.setMilliseconds(0);
  92. return Math.floor(today.getTime() / 1000);
  93. }
  94. //获取当前小时数
  95. , getNowHour: function () {
  96. var today = new Date();
  97. today.setMilliseconds(0);
  98. today.setSeconds(0);
  99. today.setMinutes(0);
  100. return Math.floor(today.getTime() / 1000);
  101. }
  102. , getNowMinuteByTimestamp: function (timestamp) {
  103. var today = new Date(timestamp * 1000);
  104. today.setSeconds(0);
  105. today.setMilliseconds(0);
  106. return Math.floor(today.getTime() / 1000);
  107. }
  108. // 文件名
  109. , getFileName: function () {
  110. var timestamp = this.getNowTimestamp();
  111. function add0(m) { return m < 10 ? '0' + m : m }
  112. var time = new Date(timestamp * 1000);
  113. var y = time.getFullYear();
  114. var m = time.getMonth() + 1;
  115. var d = time.getDate();
  116. var h = time.getHours();
  117. var mm = time.getMinutes();
  118. var s = time.getSeconds();
  119. return y + '' + add0(m) + '' + add0(d) + '' + add0(h) + '' + add0(mm) + '' + add0(s);
  120. }
  121. , getNowTimeTag: function () {
  122. var timestamp = this.getNowTimestamp();
  123. function add0(m) { return m < 10 ? '0' + m : m }
  124. var time = new Date(timestamp * 1000);
  125. var y = time.getFullYear();
  126. var m = time.getMonth() + 1;
  127. var d = time.getDate();
  128. var h = time.getHours();
  129. var mm = time.getMinutes();
  130. var s = time.getSeconds();
  131. return y + '年' + add0(m) + '月' + add0(d) + '日';
  132. }
  133. //友好时间
  134. ,dateStr: function(date){
  135. //获取js 时间戳
  136. var time=new Date().getTime();
  137. //去掉 js 时间戳后三位,与php 时间戳保持一致
  138. time=parseInt((time-date*1000)/1000);
  139. //存储转换值
  140. var s;
  141. if(time<60*10){//十分钟内
  142. return '刚刚';
  143. }else if((time<60*60)&&(time>=60*10)){
  144. //超过十分钟少于1小时
  145. s = Math.floor(time/60);
  146. return s+"分钟前";
  147. }else if((time<60*60*24)&&(time>=60*60)){
  148. //超过1小时少于24小时
  149. s = Math.floor(time/60/60);
  150. return s+"小时前";
  151. }else if((time<60*60*24*3)&&(time>=60*60*24)){
  152. //超过1天少于3天内
  153. s = Math.floor(time/60/60/24);
  154. return s+"天前";
  155. }else{
  156. //超过3天
  157. var date= new Date(parseInt(date) * 1000);
  158. return date.getFullYear()+"/"+(date.getMonth()+1)+"/"+date.getDate();
  159. }
  160. }
  161. }