date.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import Vue from "vue";
  2. import dayjs from "dayjs";
  3. // 默认中文
  4. require("dayjs/locale/zh-cn");
  5. dayjs.locale("zh-cn");
  6. Vue.prototype.$date = dayjs;
  7. Vue.filter("date", function(value) {
  8. return value && dayjs(value).format("YYYY-MM-DD");
  9. });
  10. Vue.filter("dateTime", function(value, format = "YYYY-MM-DD HH:mm:ss") {
  11. if ((value + "").length == 10) {
  12. value *= 1000;
  13. }
  14. return value && dayjs(value).format(format);
  15. });
  16. const week = "周一、周二、周三、周四、周五、周六、周曰".split("、");
  17. Vue.filter("week", function(v) {
  18. return week[v];
  19. });
  20. Vue.prototype.$dateRange = {
  21. //
  22. startToEnd: (unit) => {
  23. const v = [
  24. dayjs()
  25. .startOf(unit)
  26. .toDate(),
  27. dayjs()
  28. .endOf(unit)
  29. .toDate()
  30. ];
  31. return v;
  32. },
  33. //
  34. before: (days) => {
  35. return [
  36. dayjs()
  37. .add(days, "day")
  38. .startOf("d")
  39. .toDate(),
  40. dayjs()
  41. .endOf("d")
  42. .toDate()
  43. ];
  44. },
  45. //
  46. yesterday: (days) => {
  47. return [
  48. dayjs()
  49. .add(days, "day")
  50. .startOf("d")
  51. .toDate(),
  52. dayjs()
  53. .add(days, "day")
  54. .endOf("d")
  55. .toDate()
  56. ];
  57. }
  58. };