【React】dayjs — 格式化时间 的使用

作者 : admin 本文共542个字,预计阅读时间需要2分钟 发布时间: 2024-06-10 共3人阅读

中文文档

1.安装

Node.js 项目中使用 Day.js,只需使用 npm 安装

 npm install dayjs

其它地方使用,参考中文文档

2.引入

import dayjs from 'dayjs'

3.使用

时间格式化

dayjs().format() 
// 默认返回的是 ISO8601 格式字符串 '2020-04-02T08:02:17-05:00'

dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]') 
// 'YYYYescape 2019-01-25T00:00:00-02:00Z'

dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'

我的使用

import dayjs from 'dayjs'
 const getContent = ()=>{
      setContent([
         ...contentList,
         {
            rpid: uuidv4(),
            user: {
              uid: '30009257',
              avatar,
              uname: '黑马前端',
            },
            content: areaValue,
            ctime: dayjs(new Date()).format('MM-DD HH:mm'), //将当前时间格式化指定格式
            like: 66,
            },])
          }
本站无任何商业行为
个人在线分享 » 【React】dayjs — 格式化时间 的使用
E-->