格式化日期方法

1、如何实现自定义日期格式?

image

2、具体代码如下

<template>
    <div class="time-box">{{ formattedDate }}</div>
  </template>
<script setup>


const currentDate = ref(new Date());
//格式化日期方法
const formatDate = (date) =>{
   const year = date.getFullYear();
   const month = String(date.getMonth() + 1).padStart(2, "0");
   const day = String(date.getDate()).padStart(2, "0");
   const hours = String(date.getHours()).padStart(2, "0");
   const minutes = String(date.getMinutes()).padStart(2, "0");
   const seconds = String(date.getSeconds()).padStart(2, "0");
   const weekDays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
   const weekDay = weekDays[date.getDay()];
   return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds} ${weekDay}`;
}

//格式化后的日期
const formattedDate = computed(() => {
  return formatDate(currentDate.value);
})
</script>
posted on 2025-08-14 17:40  转角遇到谁  阅读(13)  评论(0)    收藏  举报