09 React使用dayjs

npm install dayjs
  1. 在 JavaScript/TypeScript 项目中使用 Day.js:
// 引入 Day.js
import dayjs from 'dayjs';

// 创建一个 Day.js 对象
const now = dayjs();

// 格式化日期
console.log(now.format('YYYY-MM-DD'));

// 添加/减去时间
console.log(now.add(1, 'day').format('YYYY-MM-DD'));

// 获取年份、月份、日期等
console.log(now.year());
console.log(now.month() + 1); // Month starts from 0, so add 1
console.log(now.date());

在 React 项目中引入 Day.js:

import React from 'react';
import dayjs from 'dayjs'; // 引入 Day.js

class MyComponent extends React.Component {
  render() {
    // 在组件中使用 Day.js
    const now = dayjs();
    return (
      <div>
        <p>当前日期:{now.format('YYYY-MM-DD')}</p>
      </div>
    );
  }
}

export default MyComponent;

相关推荐

  1. 09 React使用dayjs

    2024-03-27 02:32:01       20 阅读
  2. Reactdayjs -- 格式化时间 的使用

    2024-03-27 02:32:01       8 阅读
  3. dayjs使用

    2024-03-27 02:32:01       35 阅读
  4. 08 React 使用uuid示例

    2024-03-27 02:32:01       15 阅读
  5. 02 React 组件使用

    2024-03-27 02:32:01       22 阅读
  6. day03--react中setState的使用

    2024-03-27 02:32:01       14 阅读
  7. <span style='color:red;'>react</span><span style='color:red;'>03</span>

    react03

    2024-03-27 02:32:01      18 阅读
  8. FastAPI+React全栈开发03 为什么使用MongoDB

    2024-03-27 02:32:01       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-27 02:32:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-27 02:32:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-27 02:32:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-27 02:32:01       18 阅读

热门阅读

  1. Python之数据分析二

    2024-03-27 02:32:01       17 阅读
  2. GB 16807-2009 防火膨胀密封件检测

    2024-03-27 02:32:01       17 阅读
  3. HDOJ 2078

    2024-03-27 02:32:01       16 阅读
  4. UR5 机器人 URDF 代码阅读

    2024-03-27 02:32:01       16 阅读