CommonMethods.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // 处理多次点击
  2. function noMultipleClicks(methods) {
  3. //复制this对象为that指向当时的this,防止找不到所需调用页面的页面对象
  4. let that = this;
  5. if (that.noClick) {
  6. that.noClick = false;
  7. //点击方法
  8. methods();
  9. setTimeout(function() {
  10. that.noClick = true;
  11. }, 2000)
  12. } else {
  13. console.log("请稍后点击")
  14. }
  15. }
  16. //格式化时间方法
  17. function dateUtil(fmt) {
  18. let o = {
  19. "M+": this.getMonth() + 1, //月份
  20. "d+": this.getDate(), //日
  21. "h+": this.getHours(), //小时
  22. "m+": this.getMinutes(), //分
  23. "s+": this.getSeconds(), //秒
  24. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  25. "S": this.getMilliseconds() //毫秒
  26. };
  27. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  28. for (let k in o)
  29. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) :
  30. (("00" + o[k]).substr(("" + o[k]).length)));
  31. return fmt;
  32. }
  33. //获取近几天时间数组
  34. function getRecentDateArray(day) {
  35. let recentDate = new Array();
  36. for (let i = day; i > 0; i--) {
  37. let dayTime = new Date();
  38. dayTime.setDate(dayTime.getDate() - i + 1);
  39. recentDate.push(dayTime.format("M-dd"));
  40. }
  41. return recentDate
  42. }
  43. //时间戳转换为时间
  44. function timeStampToTime(timestamp) {
  45. timestamp = timestamp ? timestamp : null;
  46. let date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  47. let Y = date.getFullYear() + '-';
  48. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  49. let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  50. let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  51. let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  52. let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  53. // return Y + M + D + h + m + s;
  54. return M + D + h + m;
  55. }
  56. export default {
  57. noMultipleClicks,
  58. dateUtil,
  59. getRecentDateArray,
  60. timeStampToTime
  61. }