uni-dateformat.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <text>{{dateShow}}</text>
  3. </template>
  4. <script>
  5. /**
  6. * Dateformat 日期格式化
  7. * @description 日期格式化组件
  8. * @tutorial https://ext.dcloud.net.cn/plugin?id=xxx
  9. * @property {Object|String|Number} date 日期对象/日期字符串/时间戳
  10. * @property {String} locale 格式化使用的语言
  11. * @value zh 中文
  12. * @value en 英文
  13. * @property {Array} threshold 应用不同类型格式化的阈值
  14. * @property {String} format 输出日期字符串时的格式
  15. */
  16. import {
  17. friendlyDate
  18. } from './date-format.js'
  19. export default {
  20. name: 'uniDateformat',
  21. props: {
  22. date: {
  23. type: [Object, String, Number],
  24. default () {
  25. return Date.now()
  26. }
  27. },
  28. locale: {
  29. type: String,
  30. default: 'zh',
  31. },
  32. threshold: {
  33. type: Array,
  34. default () {
  35. return [0, 0]
  36. }
  37. },
  38. format: {
  39. type: String,
  40. default: 'yyyy/MM/dd hh:mm:ss'
  41. },
  42. // refreshRate使用不当可能导致性能问题,谨慎使用
  43. refreshRate: {
  44. type: [Number, String],
  45. default: 0
  46. }
  47. },
  48. data() {
  49. return {
  50. refreshMark: 0
  51. }
  52. },
  53. computed: {
  54. dateShow() {
  55. this.refreshMark
  56. return friendlyDate(this.date, {
  57. locale: this.locale,
  58. threshold: this.threshold,
  59. format: this.format
  60. })
  61. }
  62. },
  63. watch: {
  64. refreshRate: {
  65. handler() {
  66. this.setAutoRefresh()
  67. },
  68. immediate: true
  69. }
  70. },
  71. methods: {
  72. refresh() {
  73. this.refreshMark++
  74. },
  75. setAutoRefresh() {
  76. clearInterval(this.refreshInterval)
  77. if (this.refreshRate) {
  78. this.refreshInterval = setInterval(() => {
  79. this.refresh()
  80. }, parseInt(this.refreshRate))
  81. }
  82. }
  83. }
  84. }
  85. </script>
  86. <style>
  87. </style>