当前位置:   article > 正文

vue3+ts实现视频根据时间轴截取,并可以通过传入截取起止时间进行当前剪辑的回显_现视频根据时间轴截取,并可以通过传入截取起止时间进行当前剪辑的回显

现视频根据时间轴截取,并可以通过传入截取起止时间进行当前剪辑的回显

 公司提出想做一个视频编辑功能,每次只裁剪一段即可,UI同时也想实现时间轴为关键帧图片的效果,从网上也没找到合适的组件,简单思考后觉得并不难,决定自己封装一个吧。组件涉及到的只有vue3+ts+scss,没有使用其他插件。

穿插一个简化版本,时间轴是一条线,功能比这个简化,或许会符合部分人的需求。

链接vue3+ts实现视频根据时间轴截取_vue项目截取一段正在播放的视频_一个人的咖啡~的博客-CSDN博客

功能概述

通过传入源视频时长,源视频的视频地址,当前剪辑的开始时间,当前剪辑的结束时间和关键帧缩略图(需要20张图片,后端提供,根据视频时长分为20节,每节取一张图)五个必传参数,视频地址将通过video标签播放,组件尺寸为100%,根据父级组件的宽度自动撑满。

时间轴模块,会根据传入的起止时间自动换算出1px===毫秒数,起止时间间隔我设置了1秒以上,开始时间拖动到结束时间前一秒左右将停止移动,结束时间拖动到开始时间后一秒左右将无法拖动,拖动开始时间时会自动将video标签的开始播放时间定位到截取的开始时间,设置结束时间后,video播放到截取的结束时间后会自动暂停,这时video标签将只能播放所截取的起止时间范围的视频。最后设置了回调queryTime(),通过回调将起止时间传出,我的业务中视频截取是后端操作,前端只需要提供截取的起止时间即可,具体看代码,如下:

参数描述
endTime视频结束时间,精确到毫秒
url视频地址,将通过video标签展示
spliterStartTime视频截取开始时间
spliterEndTime视频截取结束时间
photoList时间轴缩略图列表

回调描述
回调方法回调参数(形参)参数描述
queryTimeArray[开始时间,结束时间]

template部分

  1. <template>
  2. <video id="videoPlayer" @play="onplay" controls="true" preload="auto" muted class="video" width="100%"
  3. :src="props.url"></video>
  4. <ul class="time-list">
  5. <li v-for="item in data.timeList" :key="item">{{item}}</li>
  6. </ul>
  7. <div class="crop-filter">
  8. <div class="timer-shaft" ref="shaft">
  9. <div class="white-shade" :style="{width:(data.endLeft-data.startLeft+12)+'px',left:data.startLeft-6+'px'}">
  10. </div>
  11. <div class="left-shade" :style="{width: (data.startLeft-6)+'px'}"></div>
  12. <div class="right-shade" :style="{width: (shaft?.clientWidth-data.endLeft-6) +'px'}"></div>
  13. <div class="strat-circle circle" ref="start" @mousedown="startMouseDown">
  14. <div class="center"></div>
  15. </div>
  16. <div class="end-circle circle" ref="end" @mousedown="endMouseDown">
  17. <div class="center"></div>
  18. </div>
  19. <!-- 此处src应绑定item -->
  20. <img @dragstart.prevent style="width: 5%;user-select: none;" v-for="item in props.photoList"
  21. src="../../../public/favicon.ico" alt="">
  22. </div>
  23. </div>
  24. </template>

分为三个部分,上面是video标签,中间是根据总时长处理出的时间数组,下面是时间轴。

script部分

  1. <!-- 起止时间间隔最小≈1秒 -->
  2. <script setup lang="ts">
  3. import {
  4. getNowTime,
  5. dateStrChangeTimeTamp,
  6. cropFilter,
  7. videoRef,
  8. } from '@/types/type'
  9. // 进度条dom
  10. const shaft = ref(null);
  11. // 开始按钮dom
  12. const start = ref(null);
  13. // 结束按钮dom
  14. const end = ref(null);
  15. const data = reactive(new cropFilter)
  16. // props参数类型
  17. interface Props {
  18. startTime ? : string;
  19. endTime: string;
  20. url: string;
  21. spliterStartTime ? : string;
  22. spliterEndTime: string;
  23. // 此处为模拟
  24. photoList: string[];
  25. }
  26. // 设置默认值,需要显式的开启,具体查看vue3文档
  27. const props = withDefaults(defineProps < Props > (), {
  28. startTime: '00:00:00.0',
  29. endTime: '00:00:08.0',
  30. spliterStartTime: '00:00:00.0',
  31. spliterEndTime: '00:00:08.0',
  32. url: '',
  33. photoList: [],
  34. })
  35. const emit = defineEmits(['queryTime'])
  36. onMounted(() => {
  37. // 随便拼一个1970年以后的年月日字符串+' '
  38. let str = '1970-01-02 '
  39. let time = dateStrChangeTimeTamp(str + props.endTime) - dateStrChangeTimeTamp(str + props.startTime)
  40. data.roal = time / shaft.value.clientWidth
  41. // 结束毫秒数
  42. let endM = (dateStrChangeTimeTamp('1970-01-02 ' + props?.spliterEndTime) - (1000 * 60 * 60 * 16))
  43. // 开始毫秒数
  44. let startM = (dateStrChangeTimeTamp('1970-01-02 ' + (props?.spliterStartTime)) - (1000 * 60 * 60 * 16))
  45. console.log(startM, endM)
  46. // 设置开始结束位置
  47. start.value.style.left = startM / data.roal - (end.value.clientWidth / 2) + 'px'
  48. end.value.style.left = endM / data.roal - (end.value.clientWidth / 2) + 'px'
  49. data.endLeft = end.value.offsetLeft
  50. data.endright = shaft.value.clientWidth - (end.value.clientWidth / 2)
  51. data.startLeft = start.value.offsetLeft + (start.value.clientWidth / 2)
  52. getVideoTime()
  53. data.timeList.push(props.startTime)
  54. let paragraph = (dateStrChangeTimeTamp(str + props.endTime) - (1000 * 60 * 60 * 16)) / 5
  55. for (let i = 1; i < 6; i++) {
  56. data.timeList.push(getNowTime(paragraph * i))
  57. }
  58. })
  59. // 播放事件
  60. const onplay = () => {
  61. let myVideo: videoRef = document.getElementById('videoPlayer');
  62. // 开始秒数
  63. let startM = (dateStrChangeTimeTamp('1970-01-02 ' + (data.startTime ? data.startTime : props
  64. .spliterStartTime)) - (1000 * 60 * 60 * 16)) / 1000
  65. // 结束秒数
  66. let endM = (dateStrChangeTimeTamp('1970-01-02 ' + (data.endTime ? data.endTime : props
  67. .spliterEndTime)) - (1000 * 60 * 60 * 16)) / 1000
  68. // 如果当前秒数小于等于截取的开始时间,就按截取的开始时间播放,如果不是,则为继续播放
  69. if (myVideo.currentTime <= startM || myVideo.currentTime > endM) {
  70. myVideo.currentTime = startM;
  71. myVideo.play();
  72. }
  73. }
  74. // 获取视频播放时长
  75. const getVideoTime = () => {
  76. if (document.getElementById('videoPlayer')) {
  77. let videoPlayer: videoRef = document.getElementById('videoPlayer');
  78. videoPlayer.addEventListener('timeupdate', function() {
  79. // 结束秒数
  80. let endM = (dateStrChangeTimeTamp('1970-01-02 ' + (data.endTime ? data.endTime : props
  81. .spliterEndTime)) - (1000 * 60 * 60 * 16)) / 1000
  82. // 如果当前播放时间大于等于截取的结束秒数,就暂停
  83. if (videoPlayer.currentTime >= endM) {
  84. videoPlayer.pause()
  85. }
  86. }, false)
  87. }
  88. }
  89. //设置播放点
  90. const playBySeconds = (num: number) => {
  91. if (num && document.getElementById('videoPlayer')) {
  92. let myVideo: videoRef = document.getElementById('videoPlayer');
  93. myVideo.currentTime = num;
  94. }
  95. }
  96. // 起始按钮
  97. const startMouseDown = (e) => {
  98. let odiv = e.currentTarget; //获取目标父元素
  99. //算出鼠标相对元素的位置
  100. let disX = e.clientX - odiv.offsetLeft;
  101. document.onmousemove = (e) => { //鼠标按下并移动的事件
  102. //用鼠标的位置减去鼠标相对元素的位置,得到元素的位置
  103. let left = e.clientX - disX;
  104. //移动当前元素
  105. odiv.style.left = left + 'px';
  106. //获取距离窗口宽度
  107. let mas = odiv.offsetLeft;
  108. if (mas <= -(start.value.clientWidth / 2)) {
  109. odiv.style.left = -(start.value.clientWidth / 2) + 'px';
  110. } else if (mas >= (data.endLeft - Math.ceil(1000 / data.roal))) {
  111. odiv.style.left = (data.endLeft - Math.ceil(1000 / data.roal)) + 'px';
  112. }
  113. data.startTime = getNowTime(data.roal * Math.floor(start.value.offsetLeft + (start.value.clientWidth /
  114. 2)))
  115. data.startLeft = start.value.clientWidth + start.value.offsetLeft
  116. // 开始秒数
  117. let startM = (dateStrChangeTimeTamp('1970-01-02 ' + (data.startTime ? data.startTime : props
  118. .spliterStartTime)) - (1000 * 60 * 60 * 16)) / 1000
  119. playBySeconds(startM)
  120. };
  121. document.onmouseup = (e) => {
  122. document.onmousemove = null;
  123. document.onmouseup = null;
  124. handleTime()
  125. };
  126. }
  127. // 结束按钮
  128. const endMouseDown = (e) => {
  129. let odiv = e.currentTarget; //获取目标父元素
  130. //算出鼠标相对元素的位置
  131. let disX = e.clientX - odiv.offsetLeft;
  132. document.onmousemove = (e) => { //鼠标按下并移动的事件
  133. //用鼠标的位置减去鼠标相对元素的位置,得到元素的位置
  134. let left = e.clientX - disX;
  135. //移动当前元素
  136. odiv.style.left = left + 'px';
  137. //获取距离窗口宽度
  138. let mas = odiv.offsetLeft;
  139. if (mas <= (data.startLeft - end.value.clientWidth + Math.ceil(1000 / data.roal))) {
  140. odiv.style.left = (data.startLeft - end.value.clientWidth + Math.ceil(1000 / data.roal)) +
  141. 'px';
  142. } else if (mas >= data.endright) {
  143. odiv.style.left = data.endright + 'px';
  144. }
  145. data.endTime = getNowTime(data.roal * Math.floor(end.value.offsetLeft + (end.value.clientWidth / 2)))
  146. data.endLeft = end.value.offsetLeft
  147. };
  148. document.onmouseup = (e) => {
  149. document.onmousemove = null;
  150. document.onmouseup = null;
  151. handleTime()
  152. };
  153. }
  154. // 传出起止时间的回调
  155. const handleTime = () => {
  156. let arr = [data.startTime, data.endTime]
  157. emit('queryTime', arr)
  158. }
  159. </script>

css部分

  1. <style scoped lang="scss">
  2. .video {
  3. width: 100%;
  4. margin-bottom: 0.2rem;
  5. }
  6. .time-list {
  7. width: 100%;
  8. color: #C0C0C0;
  9. font-size: 0.12rem;
  10. margin-bottom: 0.1rem;
  11. display: flex;
  12. align-items: center;
  13. justify-content: space-between;
  14. }
  15. .crop-filter {
  16. width: 100%;
  17. padding: 0 0.1rem;
  18. box-sizing: border-box;
  19. display: flex;
  20. align-items: center;
  21. .timer-shaft {
  22. width: 100%;
  23. position: relative;
  24. .circle {
  25. width: 0.2rem;
  26. position: absolute;
  27. top: -8%;
  28. height: 110%;
  29. background-color: #ffffff;
  30. cursor: e-resize;
  31. display: flex;
  32. align-items: center;
  33. justify-content: center;
  34. .center {
  35. width: 0.02rem;
  36. height: 0.15rem;
  37. background-color: #D8D8D8;
  38. }
  39. }
  40. .strat-circle {
  41. left: -0.09rem;
  42. border-radius: 0.03rem 0 0 0.03rem;
  43. }
  44. .end-circle {
  45. right: -0.1rem;
  46. border-radius: 0 0.03rem 0.03rem 0;
  47. }
  48. .white-shade {
  49. position: absolute;
  50. top: -8%;
  51. height: 110%;
  52. width: 100%;
  53. background-color: transparent;
  54. border: 0.04rem solid #fff;
  55. box-sizing: border-box;
  56. border-left: 0;
  57. border-right: 0;
  58. }
  59. .left-shade {
  60. position: absolute;
  61. left: 0;
  62. top: 0;
  63. height: 100%;
  64. background: rgba(0, 0, 0, 0.6);
  65. }
  66. .right-shade {
  67. position: absolute;
  68. right: 0;
  69. top: 0;
  70. height: 100%;
  71. background: rgba(0, 0, 0, 0.6);
  72. }
  73. }
  74. }
  75. </style>

.vue完整代码

  1. <template>
  2. <video id="videoPlayer" @play="onplay" controls="true" preload="auto" muted class="video" width="100%"
  3. :src="props.url"></video>
  4. <ul class="time-list">
  5. <li v-for="item in data.timeList" :key="item">{{item}}</li>
  6. </ul>
  7. <div class="crop-filter">
  8. <div class="timer-shaft" ref="shaft">
  9. <div class="white-shade" :style="{width:(data.endLeft-data.startLeft+12)+'px',left:data.startLeft-6+'px'}">
  10. </div>
  11. <div class="left-shade" :style="{width: (data.startLeft-6)+'px'}"></div>
  12. <div class="right-shade" :style="{width: (shaft?.clientWidth-data.endLeft-6) +'px'}"></div>
  13. <div class="strat-circle circle" ref="start" @mousedown="startMouseDown">
  14. <div class="center"></div>
  15. </div>
  16. <div class="end-circle circle" ref="end" @mousedown="endMouseDown">
  17. <div class="center"></div>
  18. </div>
  19. <!-- 此处src应绑定item -->
  20. <img @dragstart.prevent style="width: 5%;user-select: none;" v-for="item in props.photoList"
  21. src="../../../public/favicon.ico" alt="">
  22. </div>
  23. </div>
  24. </template>
  25. <!-- 起止时间间隔最小≈1秒 -->
  26. <script setup lang="ts">
  27. import {
  28. getNowTime,
  29. dateStrChangeTimeTamp,
  30. cropFilter,
  31. videoRef,
  32. } from '@/types/type'
  33. // 进度条dom
  34. const shaft = ref(null);
  35. // 开始按钮dom
  36. const start = ref(null);
  37. // 结束按钮dom
  38. const end = ref(null);
  39. const data = reactive(new cropFilter)
  40. // props参数类型
  41. interface Props {
  42. startTime ? : string;
  43. endTime: string;
  44. url: string;
  45. spliterStartTime ? : string;
  46. spliterEndTime: string;
  47. // 此处为模拟
  48. photoList: string[];
  49. }
  50. // 设置默认值,需要显式的开启,具体查看vue3文档
  51. const props = withDefaults(defineProps < Props > (), {
  52. startTime: '00:00:00.0',
  53. endTime: '00:00:08.0',
  54. spliterStartTime: '00:00:00.0',
  55. spliterEndTime: '00:00:08.0',
  56. url: '',
  57. photoList: [],
  58. })
  59. const emit = defineEmits(['queryTime'])
  60. onMounted(() => {
  61. // 随便拼一个1970年以后的年月日字符串+' '
  62. let str = '1970-01-02 '
  63. let time = dateStrChangeTimeTamp(str + props.endTime) - dateStrChangeTimeTamp(str + props.startTime)
  64. data.roal = time / shaft.value.clientWidth
  65. // 结束毫秒数
  66. let endM = (dateStrChangeTimeTamp('1970-01-02 ' + props?.spliterEndTime) - (1000 * 60 * 60 * 16))
  67. // 开始毫秒数
  68. let startM = (dateStrChangeTimeTamp('1970-01-02 ' + (props?.spliterStartTime)) - (1000 * 60 * 60 * 16))
  69. console.log(startM, endM)
  70. // 设置开始结束位置
  71. start.value.style.left = startM / data.roal - (end.value.clientWidth / 2) + 'px'
  72. end.value.style.left = endM / data.roal - (end.value.clientWidth / 2) + 'px'
  73. data.endLeft = end.value.offsetLeft
  74. data.endright = shaft.value.clientWidth - (end.value.clientWidth / 2)
  75. data.startLeft = start.value.offsetLeft + (start.value.clientWidth / 2)
  76. getVideoTime()
  77. data.timeList.push(props.startTime)
  78. let paragraph = (dateStrChangeTimeTamp(str + props.endTime) - (1000 * 60 * 60 * 16)) / 5
  79. for (let i = 1; i < 6; i++) {
  80. data.timeList.push(getNowTime(paragraph * i))
  81. }
  82. })
  83. // 播放事件
  84. const onplay = () => {
  85. let myVideo: videoRef = document.getElementById('videoPlayer');
  86. // 开始秒数
  87. let startM = (dateStrChangeTimeTamp('1970-01-02 ' + (data.startTime ? data.startTime : props
  88. .spliterStartTime)) - (1000 * 60 * 60 * 16)) / 1000
  89. // 结束秒数
  90. let endM = (dateStrChangeTimeTamp('1970-01-02 ' + (data.endTime ? data.endTime : props
  91. .spliterEndTime)) - (1000 * 60 * 60 * 16)) / 1000
  92. // 如果当前秒数小于等于截取的开始时间,就按截取的开始时间播放,如果不是,则为继续播放
  93. if (myVideo.currentTime <= startM || myVideo.currentTime > endM) {
  94. myVideo.currentTime = startM;
  95. myVideo.play();
  96. }
  97. }
  98. // 获取视频播放时长
  99. const getVideoTime = () => {
  100. if (document.getElementById('videoPlayer')) {
  101. let videoPlayer: videoRef = document.getElementById('videoPlayer');
  102. videoPlayer.addEventListener('timeupdate', function() {
  103. // 结束秒数
  104. let endM = (dateStrChangeTimeTamp('1970-01-02 ' + (data.endTime ? data.endTime : props
  105. .spliterEndTime)) - (1000 * 60 * 60 * 16)) / 1000
  106. // 如果当前播放时间大于等于截取的结束秒数,就暂停
  107. if (videoPlayer.currentTime >= endM) {
  108. videoPlayer.pause()
  109. }
  110. }, false)
  111. }
  112. }
  113. //设置播放点
  114. const playBySeconds = (num: number) => {
  115. if (num && document.getElementById('videoPlayer')) {
  116. let myVideo: videoRef = document.getElementById('videoPlayer');
  117. myVideo.currentTime = num;
  118. }
  119. }
  120. // 起始按钮
  121. const startMouseDown = (e) => {
  122. let odiv = e.currentTarget; //获取目标父元素
  123. //算出鼠标相对元素的位置
  124. let disX = e.clientX - odiv.offsetLeft;
  125. document.onmousemove = (e) => { //鼠标按下并移动的事件
  126. //用鼠标的位置减去鼠标相对元素的位置,得到元素的位置
  127. let left = e.clientX - disX;
  128. //移动当前元素
  129. odiv.style.left = left + 'px';
  130. //获取距离窗口宽度
  131. let mas = odiv.offsetLeft;
  132. if (mas <= -(start.value.clientWidth / 2)) {
  133. odiv.style.left = -(start.value.clientWidth / 2) + 'px';
  134. } else if (mas >= (data.endLeft - Math.ceil(1000 / data.roal))) {
  135. odiv.style.left = (data.endLeft - Math.ceil(1000 / data.roal)) + 'px';
  136. }
  137. data.startTime = getNowTime(data.roal * Math.floor(start.value.offsetLeft + (start.value.clientWidth /
  138. 2)))
  139. data.startLeft = start.value.clientWidth + start.value.offsetLeft
  140. // 开始秒数
  141. let startM = (dateStrChangeTimeTamp('1970-01-02 ' + (data.startTime ? data.startTime : props
  142. .spliterStartTime)) - (1000 * 60 * 60 * 16)) / 1000
  143. playBySeconds(startM)
  144. };
  145. document.onmouseup = (e) => {
  146. document.onmousemove = null;
  147. document.onmouseup = null;
  148. handleTime()
  149. };
  150. }
  151. // 结束按钮
  152. const endMouseDown = (e) => {
  153. let odiv = e.currentTarget; //获取目标父元素
  154. //算出鼠标相对元素的位置
  155. let disX = e.clientX - odiv.offsetLeft;
  156. document.onmousemove = (e) => { //鼠标按下并移动的事件
  157. //用鼠标的位置减去鼠标相对元素的位置,得到元素的位置
  158. let left = e.clientX - disX;
  159. //移动当前元素
  160. odiv.style.left = left + 'px';
  161. //获取距离窗口宽度
  162. let mas = odiv.offsetLeft;
  163. if (mas <= (data.startLeft - end.value.clientWidth + Math.ceil(1000 / data.roal))) {
  164. odiv.style.left = (data.startLeft - end.value.clientWidth + Math.ceil(1000 / data.roal)) +
  165. 'px';
  166. } else if (mas >= data.endright) {
  167. odiv.style.left = data.endright + 'px';
  168. }
  169. data.endTime = getNowTime(data.roal * Math.floor(end.value.offsetLeft + (end.value.clientWidth / 2)))
  170. data.endLeft = end.value.offsetLeft
  171. };
  172. document.onmouseup = (e) => {
  173. document.onmousemove = null;
  174. document.onmouseup = null;
  175. handleTime()
  176. };
  177. }
  178. // 传出起止时间的回调
  179. const handleTime = () => {
  180. let arr = [data.startTime, data.endTime]
  181. emit('queryTime', arr)
  182. }
  183. </script>
  184. <style scoped lang="scss">
  185. .video {
  186. width: 100%;
  187. margin-bottom: 0.2rem;
  188. }
  189. .time-list {
  190. width: 100%;
  191. color: #C0C0C0;
  192. font-size: 0.12rem;
  193. margin-bottom: 0.1rem;
  194. display: flex;
  195. align-items: center;
  196. justify-content: space-between;
  197. }
  198. .crop-filter {
  199. width: 100%;
  200. padding: 0 0.1rem;
  201. box-sizing: border-box;
  202. display: flex;
  203. align-items: center;
  204. .timer-shaft {
  205. width: 100%;
  206. position: relative;
  207. .circle {
  208. width: 0.2rem;
  209. position: absolute;
  210. top: -8%;
  211. height: 110%;
  212. background-color: #ffffff;
  213. cursor: e-resize;
  214. display: flex;
  215. align-items: center;
  216. justify-content: center;
  217. .center {
  218. width: 0.02rem;
  219. height: 0.15rem;
  220. background-color: #D8D8D8;
  221. }
  222. }
  223. .strat-circle {
  224. left: -0.09rem;
  225. border-radius: 0.03rem 0 0 0.03rem;
  226. }
  227. .end-circle {
  228. right: -0.1rem;
  229. border-radius: 0 0.03rem 0.03rem 0;
  230. }
  231. .white-shade {
  232. position: absolute;
  233. top: -8%;
  234. height: 110%;
  235. width: 100%;
  236. background-color: transparent;
  237. border: 0.04rem solid #fff;
  238. box-sizing: border-box;
  239. border-left: 0;
  240. border-right: 0;
  241. }
  242. .left-shade {
  243. position: absolute;
  244. left: 0;
  245. top: 0;
  246. height: 100%;
  247. background: rgba(0, 0, 0, 0.6);
  248. }
  249. .right-shade {
  250. position: absolute;
  251. right: 0;
  252. top: 0;
  253. height: 100%;
  254. background: rgba(0, 0, 0, 0.6);
  255. }
  256. }
  257. }
  258. </style>

type.ts代码

  1. export interface videoRef {
  2. // 其他冗余字段
  3. [propName: string]: any;
  4. // 数字值,表示当前播放的时间,以秒计
  5. currentTime: number;
  6. }
  7. export class cropFilter {
  8. // 结束按钮距离左侧距离
  9. endLeft: string | number = 0;
  10. // 结束按钮初始位置
  11. endright: string | number = 0;
  12. // 开始按钮距离左侧距离
  13. startLeft: string | number = 0;
  14. // 毫秒/px(1px===的毫秒数)
  15. roal: string | number = 0;
  16. // 开始时间
  17. startTime: string | number = 0;
  18. // 结束时间
  19. endTime: string | number = 0;
  20. // 时间轴显示时间数组
  21. timeList: string[] = [];
  22. }
  23. //日期字符串转成时间戳
  24. export function dateStrChangeTimeTamp(dateStr: string) {
  25. dateStr = dateStr.substring(0, 23);
  26. dateStr = dateStr.replace(/-/g, '/');
  27. let timeTamp = new Date(dateStr).getTime();
  28. return timeTamp
  29. }
  30. // 精准到毫秒
  31. export function getNowTime(val: string | number) {
  32. const date = new Date(val)
  33. const hour = (date.getHours() - 8) < 10 ? '0' + (date.getHours() - 8) : date.getHours() - 8
  34. const minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  35. const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  36. const milliSeconds = date.getMilliseconds() //毫秒
  37. const currentTime = hour + ':' + minute + ':' + second + '.' + milliSeconds
  38. console.log(currentTime)
  39. return currentTime
  40. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/130221
推荐阅读
相关标签
  

闽ICP备14008679号