当前位置: 首页 > news >正文

Vue3.2+TS在v-for的时候,循环处理时间,将其变成xx-xx-xx xx:xx:xx格式,最后教给大家自己封装一个时间hooks,直接复用

Vue3.2+TS在v-for的时候,循环处理时间,将其变成xx-xx-xx xx:xx:xx格式

最后教给大家自己封装一个时间hooks,直接复用

1.没有封装,直接使用
<template><div><ul><li v-for="item,index in arr" :key="item">{{index}}-----{{item}}---{{formateDate(item)}}</li></ul></div>
</template><script setup lang="ts">
const arr:string[] = ['Wed Aug 10 2023 16:51:08 GMT+0800 (中国标准时间)','Wed Aug 12 2022 16:53:03 GMT+0800 (中国标准时间)','Wed Aug 11 2021 16:54:04 GMT+0800 (中国标准时间)','Wed Aug 05 2020 16:55:03 GMT+0800 (中国标准时间)','Wed Aug 09 2019 16:56:01 GMT+0800 (中国标准时间)',
]
const formateDate = (date:string)=> {if (!date)  return '';const timer = new Date(date)let year = timer.getFullYear()let mouth = addZero(timer.getMonth() + 1)let day = addZero(timer.getDate())let hour = addZero(timer.getHours())let min = addZero(timer.getMinutes())let sec = addZero(timer.getSeconds())return `${year}-${mouth}-${day} ${hour}:${min}:${sec}`  
}const addZero = (num:number)=>{return num > 9 ? num : `0${num}`
}</script>
<style scoped lang="less">
ul{list-style-type: none;li{height: 30px;line-height: 30px;background-color: aqua;margin-bottom: 10px;}
}</style>

假如我们现在其他地方还需要用到这个,那么我们可以把这个方法封装成一个hooks,用以复用

1.在src目录下,新建一个utils文件夹,定义一个timehandle文件,将需要的时间格式转化函数放进去,然后导出。
export const formateDate = (date:string)=> {if (!date)  return '';const timer = new Date(date)let year = timer.getFullYear()let mouth = addZero(timer.getMonth() + 1)let day = addZero(timer.getDate())let hour = addZero(timer.getHours())let min = addZero(timer.getMinutes())let sec = addZero(timer.getSeconds())return `${year}-${mouth}-${day} ${hour}:${min}:${sec}`  
}const addZero = (num:number)=>{return num > 9 ? num : `0${num}`
}
2.在需要用到这个函数的组件内,将其导入
<template><div><ul><li v-for="item,index in arr" :key="item">{{index}}-----{{item}}---{{formateDate(item)}}</li></ul></div>
</template><script setup lang="ts">
import {formateDate} from './utils/timehandle'
const arr:string[] = ['Wed Aug 10 2023 16:51:08 GMT+0800 (中国标准时间)','Wed Aug 12 2022 16:53:03 GMT+0800 (中国标准时间)','Wed Aug 11 2021 16:54:04 GMT+0800 (中国标准时间)','Wed Aug 05 2020 16:55:03 GMT+0800 (中国标准时间)','Wed Aug 09 2019 16:56:01 GMT+0800 (中国标准时间)',
]</script>
<style scoped lang="less">
ul{list-style-type: none;li{height: 30px;line-height: 30px;background-color: aqua;margin-bottom: 10px;}
}</style>
http://www.lryc.cn/news/121212.html

相关文章:

  • 05 mysql innodb page
  • 记录一次electron打包提示文件找不到的解决方法
  • 《大型网站技术架构》第二篇 架构-高可用
  • VS Code 使用cnpm下载包失败
  • 【图像分类】CNN + Transformer 结合系列.4
  • 分享一下利用Vue表单处理实现复杂表单布局
  • SAP Fiori 问题收集
  • econml双机器学习实现连续干预和预测
  • 《甲午》观后感——GPT-3.5所写
  • Java技术整理(6)—— 微服务篇
  • 途乐证券-新股行情持续火爆,哪些因素影响首日表现?
  • 在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——聚合与搜索(三)
  • 基于weka手工实现KNN
  • Lua 闭包
  • Java技术整理(1)—— JVM篇
  • bug解决:AssertionError: No inf checks were recorded for this optimizer.
  • Django笔记之数据库查询优化汇总
  • JVM内存区域
  • 某行业CTF一道流量分析题
  • 【Kafka】1.Kafka简介及安装
  • Kafka API与SpringBoot调用
  • JavaScript构造函数和类的区别
  • Spring与Spring Bean
  • 并发相关面试题
  • Hadoop+Python+Django+Mysql热门旅游景点数据分析系统的设计与实现(包含设计报告)
  • php中nts和ts
  • 设计模式之责任链模式【Java实现】
  • Android 12.0 系统systemui状态栏下拉左滑显示通知栏右滑显示控制中心模块的流程分析
  • 服务器安装JDK
  • cpu查询