Date类型及dayjs的使用总结

一、Date()

Date数据类型用于处理日期和时间,它可以表示自1970年1月1日00:00:00 UTC(Coordinated Universal Time,国际协调时间)以来的毫秒数。

1.创建Date对象

例如:Tue Oct 31 2023 14:01:33 GMT+0800 (中国标准时间)

// 创建要给包含当前日期和时间的Date对象
const currentDate = new Date()
// 创建一个特定的日期和时间的Date对象, 例如:11 Fri Dec 01 2023 14:08:00 GMT+0800 (中国标准时间)
// new Date(year, month, day, hour, minute, second, millisecond)
const specificDate1 = new Date(2023, 10, 31, 14, 8);
const specificDate2 = new Date('2023-10-31 14:8:00');

2.获取Date对象的值

getMonth 方法返回一个处于 0 到 11 之间的整数,它代表 Date 对象中的月份值。这个整数并不等于按照惯例来表示月份的数字,而是要比按惯例表示的值小 1

const currentDate = new Date()
const year = currentDate.getFullYear(); // 获取当前年
const month = currentDate.getMonth() + 1; // 获取当前月
const day = currentDate.getDate(); // 获取当前日
const hours = currentDate.getHours(); // 获取当前小时数
const minute = currentDate.getMinutes(); // 获取当前分钟数
const second = currentDate.getSeconds(); // 获取当前秒数

3.操作Date对象

3.1 getTime() — 获取时间戳

可以使用getTime()方法来获取Date对象的以毫秒为单位的时间戳。

const currentDate = new Date();
const timestamp = currentDate.getTime();
3.2 setFullYear(year) — 设置Date对象的年份
const currentDate = new Date();
currentDate.setFullYear(2030);

……

二、dayjs()

安装及使用

安装
使用
import dayjs from 'dayjs';

cosnt currentDate = dayjs();

image.png

2.获取dayjs对象的值

dayjs().get("year"); // 年
dayjs().get("month"); //月,真实月份需+1
dayjs().get("date"); // 日
dayjs().get("hour"); // 时
dayjs().get("minute"); // 分
dayjs().get("second"); // 秒
dayjs().get("millisecond"); // 毫秒
dayjs().get("day"); // 星期

3.各种日期转dayjs对象

// 1.普通时间转dayjs
dayjs('2023-10-31');
// 2.中国标准时间转dayjs
dayjs('Tue Oct 31 2023 14:43:58 GMT+0800');
// 3.时间戳(ms 毫秒级 13)转dayjs 
dayjs(ms); 
// 4.时间戳(s 秒级 10)转dayjs 
dayjs.unix(ms); 

4.操作dayjs对象

4.1 dayjs()对象格式化

dayjs().format(‘YYYY-MM-DD HH:mm:ss’)

4.2加减指定的时间
dayjs().add(5, 'minute'); // 当前时间加5分钟
dayjs().subtract(5, 'minute'); // 当前时间减5分钟
4.3 diff() — 两个日期直接相差多少时、分、秒
const time1 = dayjs('2022-7-20 12:00:00');
const time2 = dayjs('2023-7-20 12:00:00');
console.log(time2.diff(time1, 'hour'));
console.log(time2.diff(time1, 'minute'));
console.log(time2.diff(time1, 'second'));
4.4 isBefore()/isAfter()/isSame() — 判断两个日期大小
const currentDate = dayjs('2022-12-20');
console.log(currentDate.isBefore(dayjs('2022-01-01'))); // false
console.log(currentDate.isAfter(dayjs('2022-01-01'))); // true
console.log(currentDate.isSame(dayjs('2022-01-01'))); // false

三、常用总结

1.日期(年月日)转时间戳

const time = new Date('2023-10-31 15:00:00');
const timestamp = time.getTime();
const timestamp = time.valueOf();
const timestamp = Date.parse(time);

2.时间戳转日期(年月日)

// dayjs()
const date = dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss');
// Date()
const date = new Date(1663814983000).toLocaleString().replace(/\//g, "-");

3.时间计算

YYYY-MM-DD HH:mm:ss 格式

// 1小时内
const startTime = dayjs().subtract(1, 'hour').format('YYYY-MM-DD HH:mm:ss'); 
// 12小时内
const startTime = dayjs().subtract(12, 'hour').format('YYYY-MM-DD HH:mm:ss');
// 1天内
const startTime = dayjs().subtract(1, 'day').format('YYYY-MM-DD HH:mm:ss');
// 1周内
const startTime = dayjs().subtract(1, 'week').format('YYYY-MM-DD HH:mm:ss'); 
// 1月内
const startTime = dayjs().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss');

[startTime, dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss')]

时间戳格式

// 今天
dayjs().startOf('day').valueOf();
[dayjs().startOf('day').valueOf(), Date.now()]

// 昨天
const start = dayjs().subtract(1, 'day').startOf('day').valueOf();
const end = dayjs().subtract(1, 'day').endOf('day').valueOf();
[start, end];

// 最近24小时
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
[start, end];

// 最近7天
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
[start, end];

// 最近1个月
const end = new Date();
const start = new Date();
 start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
[start, end];

// 最近3个月
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
[start, end];

// 本周
const end = Date.now();
const start = dayjs().startOf('week').add(0, 'day').valueOf();
[start, end];

// 上周
const end = dayjs().subtract(1, 'week').endOf('week').add(0, 'day').valueOf();
const start = dayjs().subtract(1, 'week').startOf('week').add(0, 'day').valueOf();
[start, end];

// 本月
const end = Date.now();
const start = dayjs().startOf('month').valueOf();
return [start, end];

// 上个月
const end = dayjs().subtract(1, 'month').endOf('month').valueOf();
const start = dayjs().subtract(1, 'month').startOf('month').valueOf();
return [start, end];

// 本季度
const end = Date.now();
const start = dayjs().startOf('quarter').valueOf();
return [start, end];
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值