Vue3 gets the current date, time and week (format: year, month, day, hour, minute, second, week)

<template>
    <div class="tri_box">
        <p>创建时间:{ { date }}</p>
    </div>
</template>
   
<script setup lang="ts">
import { ref, onMounted } from 'vue'

const date = ref('')
  //Function to format time. It accepts a parameter time of numeric type, representing the time that needs to be formatted.
  //If time is less than 10, return a string representation with leading zeros;
  //Otherwise, convert time to string and return.
function formatTime(time: number) {     return time < 10 ? `0${time}` : time } function updateTime() {     const now = new Date()     const year = now.getFullYear() //year     const month = now .getMonth() + 1 //month     const day = now.getDate() //day     const hours = now.getHours() //hours     const minutes = now.getMinutes() //minutes     const seconds = now.getSeconds () //seconds     const week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][now.












  date.value = `${year}-${formatTime(month)}-${formatTime(day)} ${formatTime(hours)}:${formatTime(minutes)}:${formatTime(seconds)} ${week}`
}
  onMounted(() => {
    updateTime()
})
</script>

Guess you like

Origin blog.csdn.net/weixin_44096999/article/details/131207411