vue格式化日期方法

调用举例

this.formatter('Fri Feb 01 2019 00:00:00 GMT+0800', 'yyyy-MM-dd hh:mm:ss')

方法

methods: {    
    formatter (thistime, fmt) {
      let $this = new Date(thistime)
      let o = {
        'M+': $this.getMonth() + 1,
        'd+': $this.getDate(),
        'h+': $this.getHours(),
        'm+': $this.getMinutes(),
        's+': $this.getSeconds(),
        'q+': Math.floor(($this.getMonth() + 3) / 3),
        'S': $this.getMilliseconds()
      }
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ($this.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      for (var k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
        }
      }
      return fmt
    }
}

代码已经进行过格式调整

Vue.js 中格式化日期,通常可以通过以下几种方式实现: ### 使用 JavaScript 的 `Date` 对象 Vue 本身没有内置的日期格式化工具,但可以使用原生的 `Date` 对象来解析和格式化日期。例如,可以创建一个方法日期转换为特定格式: ```javascript function formatDate(dateString) { const date = new Date(dateString); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始 const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } ``` 在模板中调用此函数进行格式化: ```html <p>{{ formatDate('2023-10-05T14:48:00') }}</p> ``` ### 使用过滤器(Vue 2) 在 Vue 2 中,可以通过过滤器对日期进行格式化。定义一个全局或局部过滤器: ```javascript filters: { formatDate(value) { const date = new Date(value); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } } ``` 然后在模板中使用它: ```html <p>{{ '2023-10-05T14:48:00' | formatDate }}</p> ``` ### 使用 Composition API(Vue 3) 在 Vue 3 中,可以利用 Composition API 创建一个可复用的日期格式化函数: ```javascript import { ref } from 'vue'; function useDateFormat() { function formatDate(dateString) { const date = new Date(dateString); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } return { formatDate }; } ``` 在组件中调用并使用: ```javascript export default { setup() { const { formatDate } = useDateFormat(); return { formatDate }; } } ``` 在模板中显示格式化日期: ```html <p>{{ formatDate('2023-10-05T14:48:00') }}</p> ``` ### 使用第三方库(如 `date-fns` 或 `moment.js`) 为了更强大的功能,比如本地化、相对时间等,可以引入日期处理库如 `date-fns` 或 `moment.js`。 #### 使用 `date-fns` 安装 `date-fns`: ```bash npm install date-fns ``` 然后在代码中导入并使用它: ```javascript import { format } from 'date-fns'; const formattedDate = format(new Date(), 'yyyy-MM-dd HH:mm:ss'); ``` #### 使用 `moment.js` 安装 `moment.js`: ```bash npm install moment ``` 然后在代码中使用: ```javascript import moment from 'moment'; const formattedDate = moment().format('YYYY-MM-DD HH:mm:ss'); ``` ### 总结 - 原生 `Date` 可以满足简单的日期格式化需求。 - Vue 2 支持过滤器,便于在模板中直接调用日期格式化。 - Vue 3 推荐使用 Composition API 来封装日期处理逻辑。 - 第三方库提供更丰富的日期操作功能,适合复杂场景。 如果需要更加灵活的解决方案,建议结合项目需求选择合适的方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值