Day.js 基本使用

Day.js 基本使用

一、概述

1、中文网

https://dayjs.fenxianglu.cn/

2、简介

Day.js 是一个极简的 JavaScript 库,可以为现代浏览器解析、验证、操作和显示日期和时间。

二、基本使用

1、安装

pnpm add dayjs

2、基本使用

https://dayjs.fenxianglu.cn/category/#typescript

import dayjs from "dayjs";
console.log("=====>", dayjs().format("YYYY-MM-DD HH:mm:ss"));
// =====> 2023-09-22 14:56:11

3、获取当前时间

import dayjs from "dayjs";
console.log("=====>", dayjs().format("YYYY-MM-DD HH:mm:ss"));
// =====> 2023-09-22 14:56:11

4、根据时间字符串创建时间对象

import dayjs from "dayjs";
console.log("=====>", dayjs("2018-08-08").format("YYYY-MM-DD HH:mm:ss"));
// =====> 2018-08-08 00:00:00

5、根据时间戳创建时间对象

import dayjs from "dayjs";
console.log("=====>", dayjs(1318781876406).format("YYYY-MM-DD HH:mm:ss"));
// =====> 2011-10-17 11:51:16

6、根据 Date 对象创建时间对象

import dayjs from "dayjs";
console.log("=====>", dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss"));
// =====> 2021-09-22 14:56:11

7、获取年份

import dayjs from "dayjs";
console.log("=====>", dayjs().year());
// =====> 2021

8、获取月份

import dayjs from "dayjs";
console.log("=====>", dayjs().month());
// =====> 8

9、获取日期

import dayjs from "dayjs";
console.log("=====>", dayjs().date());
// =====> 22

10、传入对象

import dayjs from "dayjs";
console.log(
  "=====>",
  dayjs({ year: 2018, month: 8, day: 8 }).format("YYYY-MM-DD HH:mm:ss")
);
// =====> 2018-08-08 00:00:00

11、传入数组

import dayjs from "dayjs";
console.log("=====>", dayjs([2018, 8, 8]).format("YYYY-MM-DD HH:mm:ss"));
// =====> 2018-08-08 00:00:00

12、UTC 时间

import dayjs from "dayjs";
console.log("=====>", dayjs.utc().format("YYYY-MM-DD HH:mm:ss"));
// =====> 2021-09-22 06:56:11

13、获取当前时间戳

import dayjs from "dayjs";
console.log("=====>", dayjs().valueOf());
// =====> 1632290171000

14、Dayjs 复制

import dayjs from "dayjs";
const dayjs1 = dayjs();
const dayjs2 = dayjs1.clone();
console.log("=====>", dayjs1 === dayjs2);
// =====> false 是两个独立的 Day.js 对象

15、dayjs 对象设置年份

import dayjs from "dayjs";
console.log("=====>", dayjs().set("year", 2018).format("YYYY-MM-DD HH:mm:ss"));
// =====> 2018-09-22 14:56:11

三、操作

1、加上

import dayjs from "dayjs";
console.log("=====>", dayjs().add(1, "year").format("YYYY-MM-DD HH:mm:ss"));
// =====> 2022-09-22 14:56:11

2、减去

import dayjs from "dayjs";
console.log(
  "=====>",
  dayjs().subtract(1, "year").format("YYYY-MM-DD HH:mm:ss")
);
// =====> 2020-09-22 14:56:11

3、时间的开始

import dayjs from "dayjs";
console.log("=====>", dayjs().startOf("year").format("YYYY-MM-DD HH:mm:ss"));
// =====> 2021-01-01 00:00:00

4、时间的结束

import dayjs from "dayjs";
console.log("=====>", dayjs().endOf("year").format("YYYY-MM-DD HH:mm:ss"));
// =====> 2021-12-31 23:59:59

四、总结

dayjs 是一个轻量的处理时间和日期的库,它的 API 设计的非常简单,使用起来也非常方便,如果你的项目中需要处理时间和日期,那么 dayjs 是一个不错的选择。更多的 API 可以参考官方文档:https://dayjs.fenxianglu.cn/

Moment.js是一个JavaScript日期处理库,它可以帮助您轻松处理日期和时间。下面是使用Moment.js的一些示例: 1. 安装Moment.js 您可以在Moment.js官方网站上下载Moment.js,或者使用npm进行安装: ``` npm install moment ``` 2. 导入Moment.js 在您的JavaScript文件中导入Moment.js: ``` import moment from 'moment'; ``` 3. 创建日期 使用moment()函数创建一个当前日期的Moment对象: ``` const now = moment(); ``` 您还可以使用moment()函数传递一个日期字符串或JavaScript Date对象来创建Moment对象: ``` const dateStr = '2022-01-01'; const date = moment(dateStr); const jsDate = new Date(); const momentDate = moment(jsDate); ``` 4. 格式化日期 使用format()函数将Moment对象格式化为字符串: ``` const nowStr = now.format('YYYY-MM-DD HH:mm:ss'); ``` 您可以使用Moment.js提供的各种格式选项来自定义日期格式。 5. 操作日期 使用add()和subtract()函数在Moment对象上添加或减去一定的时间: ``` const tomorrow = now.clone().add(1, 'day'); const lastWeek = now.clone().subtract(1, 'week'); ``` 6. 比较日期 使用isBefore()、isSame()和isAfter()函数比较两个Moment对象: ``` const date1 = moment('2022-01-01'); const date2 = moment('2022-01-02'); const isBefore = date1.isBefore(date2); // true const isSame = date1.isSame(date2, 'day'); // false const isAfter = date1.isAfter(date2); // false ``` 这些是Moment.js的一些基本用法示例。Moment.js还提供了许多其他功能,如本地化、时区处理和持续时间计算。您可以查看Moment.js官方文档以获取更多信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值