小程序端工程代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.7 KiB

  1. <template>
  2. <view class="time">
  3. {{ tipText }}
  4. <text class="styleAll" v-if="isDay === true">{{ day }}</text>
  5. <text class="timeTxt">{{ dayText }}</text>
  6. <text class="styleAll">{{ hour }}</text>
  7. <text class="timeTxt">{{ hourText }}</text>
  8. <text class="styleAll">{{ minute }}</text>
  9. <text class="timeTxt">{{ minuteText }}</text>
  10. <text class="styleAll">{{ second }}</text>
  11. <text class="timeTxt">{{ secondText }}</text>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'CountDown',
  17. props: {
  18. //距离开始提示文字
  19. tipText: {
  20. type: String,
  21. default: '倒计时',
  22. },
  23. dayText: {
  24. type: String,
  25. default: '天',
  26. },
  27. hourText: {
  28. type: String,
  29. default: '时',
  30. },
  31. minuteText: {
  32. type: String,
  33. default: '分',
  34. },
  35. secondText: {
  36. type: String,
  37. default: '秒',
  38. },
  39. datatime: {},
  40. isDay: {
  41. type: Boolean,
  42. default: true,
  43. },
  44. },
  45. data() {
  46. return {
  47. timeInterval: null,
  48. time: this.datatime,
  49. day: '00',
  50. hour: '00',
  51. minute: '00',
  52. second: '00',
  53. }
  54. },
  55. created() {
  56. this.show_time()
  57. },
  58. watch: {
  59. datatime(val) {
  60. clearInterval(this.timeInterval)
  61. this.time = val
  62. this.show_time()
  63. },
  64. },
  65. mounted() {
  66. },
  67. methods: {
  68. show_time() {
  69. console.log(this.datatime)
  70. if (this.time.toString().length == 13) {
  71. // 毫秒级
  72. console.log('毫秒')
  73. this.time = this.time / 1000
  74. } else if (this.time.toString().length == 10) {
  75. console.log('秒')
  76. // 秒级
  77. } else {
  78. // 时间
  79. console.log('时间')
  80. this.time = Date.parse(this.time) / 1000
  81. }
  82. this.runTime()
  83. this.timeInterval = setInterval(this.runTime, 1000)
  84. },
  85. runTime() {
  86. //时间函数
  87. let intDiff = this.time - Date.parse(new Date()) / 1000 //获取数据中的时间戳的时间差
  88. let day = 0,
  89. hour = 0,
  90. minute = 0,
  91. second = 0
  92. if (intDiff > 0) {
  93. //转换时间
  94. if (this.isDay === true) {
  95. day = Math.floor(intDiff / (60 * 60 * 24))
  96. } else {
  97. day = 0
  98. }
  99. hour = Math.floor(intDiff / (60 * 60)) - day * 24
  100. minute = Math.floor(intDiff / 60) - day * 24 * 60 - hour * 60
  101. second = Math.floor(intDiff) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60
  102. if (hour <= 9) hour = '0' + hour
  103. if (minute <= 9) minute = '0' + minute
  104. if (second <= 9) second = '0' + second
  105. this.day = day
  106. this.hour = hour
  107. this.minute = minute
  108. this.second = second
  109. } else {
  110. this.day = '00'
  111. this.hour = '00'
  112. this.minute = '00'
  113. this.second = '00'
  114. }
  115. }
  116. },
  117. destroyed() {
  118. clearTimeout(this.timeInterval)
  119. }
  120. }
  121. </script>