Newer
Older
urbanLifeline_YanAn / src / mixins / timeSelect.js
@zhangqy zhangqy on 3 Oct 2 KB first commit
  1. export const timeSelect = {
  2. setup() {
  3. function timestampToTime(timestamp) {
  4. var date = new Date(timestamp); // 时间戳为10位需*1000,时间戳为13位的话不需乘1000
  5. let Y = date.getFullYear() + "-";
  6. let M =
  7. (date.getMonth() + 1 < 10
  8. ? "0" + (date.getMonth() + 1)
  9. : date.getMonth() + 1) + "-";
  10. let D = date.getDate() + " ";
  11. return Y + M + D;
  12. }
  13.  
  14. // 周选择器开始时间和结束时间
  15. function formatWeekTime(val) {
  16. if (val == "") {
  17. queryParams.value.datetime = [];
  18. queryParams.value.weekStartDate = "";
  19. queryParams.value.WeekEndDate = "";
  20. return;
  21. }
  22. let todayDay = val ? new Date(val) : new Date();
  23. console.log(todayDay);
  24. let todayOfWeek = todayDay.getDay();
  25. console.log(todayOfWeek);
  26. let spendDay = 1;
  27. if (todayOfWeek !== 0) {
  28. spendDay = 7 - todayOfWeek; // 开始时间到结束时间还有几天
  29. }
  30. // 86400000为一天的毫秒数
  31. let startTimeNum = todayDay.valueOf() - (6 - spendDay) * 86400000; // 开始时间时间戳
  32. let endTimeNum = todayDay.valueOf() + spendDay * 86400000; // 结束时间时间戳
  33. let startTimeStr = timestampToTime(startTimeNum); // 时间戳转字符串
  34. let endTimeStr = timestampToTime(endTimeNum);
  35. let startDate = proxy
  36. .moment(startTimeStr)
  37. .add(5, "days")
  38. .format("YYYY-MM-DD");
  39. let endDate = proxy
  40. .moment(endTimeStr)
  41. .add(5, "days")
  42. .format("YYYY-MM-DD");
  43. queryParams.value.weekStartDate = startDate;
  44. queryParams.value.WeekEndDate = endDate;
  45. queryParams.value.datetime = [startDate, endDate];
  46. }
  47. //选择周
  48. function changeWeek() {
  49. queryParams.value.monthTime = queryParams.value.yearTime = "";
  50. formatWeekTime(queryParams.value.weekTime);
  51. }
  52. //选择月
  53. function changeMonth() {
  54. queryParams.value.weekTime = queryParams.value.yearTime = "";
  55. formatWeekTime(queryParams.value.weekTime);
  56. queryParams.value.datetime = [
  57. proxy
  58. .moment(queryParams.value.monthTime)
  59. .startOf("months")
  60. .format("YYYY-MM-DD"),
  61. proxy
  62. .moment(queryParams.value.monthTime)
  63. .endOf("months")
  64. .format("YYYY-MM-DD"),
  65. ];
  66. }
  67. //选择年
  68. function changeYear() {
  69. queryParams.value.monthTime = queryParams.value.weekTime = "";
  70. changeweek(queryParams.value.weekTime);
  71. queryParams.value.datetime = [
  72. proxy
  73. .moment(queryParams.value.yearTime)
  74. .startOf("years")
  75. .format("YYYY-MM-DD"),
  76. proxy
  77. .moment(queryParams.value.yearTime)
  78. .endOf("years")
  79. .format("YYYY-MM-DD"),
  80. ];
  81. }
  82. // 暴露出去
  83. return {
  84. changeWeek,
  85. changeMonth,
  86. changeYear,
  87. };
  88. },
  89. };