vue3时间获取

根据自己需要进行选择使用,有静态时间和动态时间

new Date():获取当前时间

getFullYear():获取年份

getMonth():获取月份

getDay():获取天

getHours():获取小时

getMinutes():获取分钟

getSeconds():获取秒数

第一种方法

<template>
   <div class="box">
        <h1>测试</h1>
        <h1>{
  { format }}</h1>
    </div>
</template>
<script setup>
// 01
const format=computed(()=>{
    const oDate=new Date()
    const year=oDate.getFullYear()
    const month=oDate.getMonth()+1
    const day=oDate.getDay().toString()
    const hour=oDate.getHours()
    const minutes=oDate.getMinutes()
    const seconds=oDate.getSeconds()
    return year+'-'+month+'-'+day+' '+hour+':'+minutes+':'+seconds
})
</script>

<style lang="less" scoped></style>

第二种方法

<template>
   <div class="box">
        <h1>测试</h1>
        <h1>{
  { formatdata }}</h1>
    </div>
</template>

<script setup>
import {ref,computed,watchEffect} from 'vue'

// 02
const formatdata=computed(()=>{
    const oDate=new Date()
    const year=oDate.getFullYear()
    const month=oDate.getMonth()+1
    const day=oDate.getDay().toString()
    const hour=oDate.getHours()
    return year+'-'+month+'-'+day
})
</script>

<style lang="less" scoped></style>

第三种方法

<template>
   <div class="box">
        <h1>测试</h1>
        <h1>{
  { currenttime }}</h1>
    </div>
</template>

<script setup>
import {ref,computed,watchEffect} from 'vue'
// 03
const currenttime=ref('')

const setdate=(oDate)=>{
    const year = oDate.getFullYear()
    const month = (oDate.getMonth() + 1).toString().padStart(2, '0')
    const day = oDate.getDate().toString().padStart(2, '0')
    const hour = oDate.getHours().toString().padStart(2, '0')
    const minutes = oDate.getMinutes().toString().padStart(2, '0')
    const seconds = oDate.getSeconds().toString().padStart(2, '0')
    return year+'-'+month+'-'+day+' '+hour+':'+minutes+':'+seconds
}

watchEffect(()=>{
    setInterval(() => {
        currenttime.value=setdate(new Date())
    }, 1000);
})
</script>

<style lang="less" scoped></style>

第四种方法

<template>
   <div class="box">
        <h1>测试</h1>
        <h2>{
  { currentDateTime }}</h2>
    </div>
</template>

<script setup>
import {ref,computed,watchEffect} from 'vue'
// 第三方插件
import dayjs from 'dayjs'

// 04
const currentDateTime = ref(dayjs().format('YYYY-MM-DD HH:mm:ss'))

watchEffect(()=>{
    setInterval(() => {
        // dayjs
        currentDateTime.value=dayjs().format('YYYY-MM-DD HH:mm:ss')
    }, 1000);
})
</script>

<style lang="less" scoped></style>

第五种方法

<template>
   <div class="box">
        <h1>测试</h1>
        // 根据拼接符号,这里是/
        <h2>{
  { formattime(time,'/') }}</h2>
    </div>
</template>

<script setup>
import {ref,computed,watchEffect} from 'vue'

// 05
const time=ref(Date.now())
// 默认为-
const formattime=(timer,flg='-')=>{
    const oDate=new Date(timer)
    const year=oDate.getFullYear()
    const month=(oDate.getMonth()+1).toString().padStart(2,'0')
    const day=oDate.getDay().toString().padStart(2,'0')
    return year+flg+month+flg+day
}
</script>

<style lang="less" scoped></style>

相关推荐

  1. vue3时间获取

    2024-01-07 15:40:03       67 阅读
  2. Vue3 时间格式化

    2024-01-07 15:40:03       25 阅读
  3. vue3获取原始值

    2024-01-07 15:40:03       32 阅读
  4. vue3中mockjs模拟获取数据

    2024-01-07 15:40:03       59 阅读
  5. vue3 循环设置 ref 并获取

    2024-01-07 15:40:03       39 阅读
  6. Vue3: 获取元素DOM的方法

    2024-01-07 15:40:03       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-07 15:40:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-07 15:40:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-07 15:40:03       87 阅读
  4. Python语言-面向对象

    2024-01-07 15:40:03       96 阅读

热门阅读

  1. 蓝桥 第二周 递归

    2024-01-07 15:40:03       57 阅读
  2. 计算机类杂志

    2024-01-07 15:40:03       45 阅读
  3. Android 打开热点2.4G系统重启解决

    2024-01-07 15:40:03       65 阅读
  4. Decontam与SCRUB:安装与使用

    2024-01-07 15:40:03       68 阅读
  5. 【网络工程师】交换机的VLAN与Trunk

    2024-01-07 15:40:03       59 阅读
  6. Redis小计(3)

    2024-01-07 15:40:03       48 阅读
  7. LeetCode //C - 933. Number of Recent Calls

    2024-01-07 15:40:03       57 阅读
  8. python&Matplotlib六:Matplotlib的图例和注释功能

    2024-01-07 15:40:03       60 阅读
  9. tf特征处理常用函数

    2024-01-07 15:40:03       61 阅读
  10. 前端要学哪些

    2024-01-07 15:40:03       58 阅读