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

news/2024/7/7 20:38:41

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://lihuaxi.xjx100.cn/news/1441411.html

相关文章

shell脚本自定义函数及其调用

1. 在util.sh脚本中自定义函数 #!/usr/bin/env bash# 检查环境变量是否存在 function assert_env_var_exist {local var_name$1if [ -z $var_name ];thenlog "ERROR: " "${BASH_SOURCE[*]}" "${LINENO}" "Failed: Check env var: $var_nam…

WiFi小工具homedale,可以切换同名WiFi节点

有一个很小众的需求&#xff0c;就是多个路由器组网时候&#xff0c;PC有时不会自动切换同名WiFi&#xff0c;homedale这个工具可以满足手动切换需求 这个界面可以看到所有节点列表&#xff0c;可以看到有很多同名的 可以选择自己想要的那个&#xff0c;比如信道/信号强度&am…

Monkey测试真的靠谱吗?

Monkey测试&#xff0c;顾名思义&#xff0c;就是模拟一只猴子在键盘上乱敲&#xff0c;从而达到测试被测系统的稳定性。Monkey测试&#xff0c;是Android自动化测试的一种手段&#xff0c;Monkey测试本身非常简单&#xff0c;Android SDK 工具支持adb Shell命令&#xff0c;实…

bs4练习

bs4练习 工具目的代码运行结果 工具 pycharm 目的 网址:http://ip.yqie.com/ipproxy.htm,原url不能用&#xff0c;更换url为&#xff1a;http://www.66ip.cn/index.html 用bs4来做一个简单的爬虫&#xff0c;爬取某个ip网址里的免费ip&#xff0c;获取每个ip的代理IP地址、端…

Vue中路由缓存问题及解决方法

一.问题 Vue Router 允许你在你的应用中创建多个视图&#xff0c;并根据路由来动态切换这些视图。默认情况下&#xff0c;当你从一个路由切换到另一个路由时&#xff0c;Vue Router 会销毁前一个路由的组件实例并创建新的组件实例。然而&#xff0c;有时候你可能希望保持一些页…

Web 自动化测试学会这一招,下班至少早一小时

♥ 前 言 大家都知道&#xff0c;我们在通过 Selenium 执行 Web 自动化测试时&#xff0c;每次都需要启动/关闭浏览器&#xff0c;如果是多线程执行还会同时打开多个&#xff0c;比较影响工作的正常进行。那有没有办法可以不用让浏览器的自动化执行干扰我们的工作呢&#xf…

剑指 Offer 46. 把数字翻译成字符串(动态规划)

文章目录 题目描述思路分析完整代码 题目描述 给定一个数字&#xff0c;我们按照如下规则把它翻译为字符串&#xff1a;0 翻译成 “a” &#xff0c;1 翻译成 “b”&#xff0c;……&#xff0c;11 翻译成 “l”&#xff0c;……&#xff0c;25 翻译成 “z”。一个数字可能有多…

Controller是单例还是多例?

Controller是单例还是多例&#xff1f; controller默认是单例的&#xff0c;不要使用非静态的成员变量&#xff0c;否则会发生数据逻辑混乱。正因为单例所以不是线程安全的。 我们下面来简单的验证下&#xff1a; package com.riemann.springbootdemo.controller;import org…