当前位置:首页 > VUE

vue实现带日期星期

2026-02-25 06:39:02VUE

实现带日期星期的 Vue 组件

使用 JavaScript 的 Date 对象获取日期和星期

在 Vue 中可以通过 JavaScript 的 Date 对象来获取当前日期和星期几的信息。以下是一个简单的实现方法:

<template>
  <div>
    {{ currentDate }} {{ currentDay }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentDate: '',
      currentDay: ''
    }
  },
  mounted() {
    this.updateDateTime()
  },
  methods: {
    updateDateTime() {
      const now = new Date()
      const days = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']

      this.currentDate = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日`
      this.currentDay = days[now.getDay()]
    }
  }
}
</script>

使用第三方库 moment.js 或 day.js

对于更复杂的日期处理,可以使用 moment.js 或 day.js 这样的日期处理库:

<template>
  <div>
    {{ formattedDate }}
  </div>
</template>

<script>
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'

dayjs.locale('zh-cn')

export default {
  data() {
    return {
      formattedDate: ''
    }
  },
  mounted() {
    this.updateDateTime()
  },
  methods: {
    updateDateTime() {
      this.formattedDate = dayjs().format('YYYY年MM月DD日 dddd')
    }
  }
}
</script>

自动更新的日期时间显示

vue实现带日期星期

如果需要日期和时间自动更新,可以添加定时器:

<script>
export default {
  data() {
    return {
      currentDateTime: ''
    }
  },
  mounted() {
    this.updateDateTime()
    setInterval(this.updateDateTime, 1000)
  },
  methods: {
    updateDateTime() {
      const now = new Date()
      const days = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']

      this.currentDateTime = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日 ${days[now.getDay()]} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
    }
  }
}
</script>

国际化日期显示

如果需要支持多种语言的日期显示,可以使用 Vue I18n 配合日期库:

vue实现带日期星期

<template>
  <div>
    {{ $d(new Date(), 'long') }}
  </div>
</template>

<script>
import { createI18n } from 'vue-i18n'

const i18n = createI18n({
  locale: 'zh-CN',
  datetimeFormats: {
    'zh-CN': {
      long: {
        year: 'numeric',
        month: 'long',
        day: 'numeric',
        weekday: 'long'
      }
    },
    'en-US': {
      long: {
        year: 'numeric',
        month: 'long',
        day: 'numeric',
        weekday: 'long'
      }
    }
  }
})

export default {
  i18n
}
</script>

注意事项

日期格式化时需要注意月份是从0开始的,所以需要加1。星期几的索引从0(星期日)到6(星期六)。

对于生产环境,推荐使用day.js代替moment.js,因为day.js体积更小,API兼容。

如果需要显示24小时制的时间,可以使用toLocaleTimeString方法或日期库的相应格式化选项。

标签: 星期日期
分享给朋友:

相关文章

vue实现日期回填

vue实现日期回填

实现日期回填的基本思路 在Vue中实现日期回填通常涉及表单数据的双向绑定,结合日期选择器组件(如Element UI的DatePicker或原生HTML的input type="date")。核心是通…

vue实现日期选择

vue实现日期选择

Vue 实现日期选择的方法 使用原生 HTML5 input[type="date"] Vue 可以直接绑定原生 HTML5 的日期选择器,适合简单场景: <template> &l…

vue实现日期加减

vue实现日期加减

实现日期加减的方法 在Vue中实现日期加减可以通过JavaScript的Date对象或第三方库如moment.js或date-fns来完成。以下是几种常见的方法: 使用JavaScript的Dat…

vue框架实现日期显示

vue框架实现日期显示

实现日期显示的基本方法 在Vue中显示日期可以通过多种方式实现,以下是几种常见的方法: 使用JavaScript的Date对象 直接通过JavaScript的Date对象获取当前日期并显示: &l…

mui vue实现日期插件

mui vue实现日期插件

MUI Vue 实现日期插件 在 Vue 项目中,可以使用 MUI(Material-UI)风格的日期选择器插件。以下是两种常见的实现方式: 使用 Vuetify 的日期选择器 Vuetify 是一…

js实现日期选择

js实现日期选择

使用原生JavaScript实现日期选择器 创建基础HTML结构 <input type="text" id="datePicker" placeholder="选择日期"> <d…