当前位置:网站首页>dayjs时间处理库的基本使用
dayjs时间处理库的基本使用
2022-08-02 00:18:00 【m0_67402013】
1获取当前时间
let a = dayjs()
2 时间格式化
let endTime = dayjs().format(‘YYYY-MM-DD’)
3 获取当前时间之前的时间day.js可以轻松获取N小时/天/月/年前等时间,如以下代码获取的是14天前的时间:
let startTime = dayjs().subtract(14,‘day’).format(‘YYYY-MM-DD’)
4.N时间后
day.js也可以轻松获取N时间后的时间,如以下代码获取的是7天后的时间:
dayjs().add(7, 'day')
5.开始时间
day.js可以获取一月/一周等的开始时间,如以下代码获取本周第一天的时间(星期一):
dayjs().startOf('week')
6.末尾时间
day.js可以获取一月/一周等的末尾时间,如以下代码获取本月最后一天时间:
dayjs().endOf('month')
7.两个时间差
day.js可以获取两个不同时间的差,如2020-09-25和2020-06-05相差112天:
const date1 = dayjs('2020-09-25');
date1.diff('2020-06-05', 'day'); // 112
8.Unix 时间戳
day.js可以轻松转换秒级和毫秒级时间戳:
dayjs().unix(); //秒
dayjs().valueOf(); //毫秒
9.获取月份天数
day.js可以轻松获取一个月的天数是30天还是31天还是28或29天。
dayjs().daysInMonth() //30,(2020年9月共30天)
10.距离当前时刻
我们经常看到显示“1分钟前”、“3小时内”等时间转换后显示效果,dayjs也可以轻松实现:
dayjs('2020-09-27 18:22:32').fromNow(); //3小时前
dayjs('2020-09-26 10:22:32').toNow(); //1天内
fromNow()
表示距离当前时刻,toNow()
表示相对当前时刻,其实感觉意思差不多。
不过要想fromNow()
和toNow()
生效,需要使用插件RelativeTime
,方法是先引入插件,再执行调用方法:
import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 期货开户调整交易所保证金标准
- Realize deletion - a specified letter in a string, such as: the string "abcd", delete the "a" letter in it, the remaining "bcd", you can also pass multiple characters to be deleted, and pass "ab" can
- What is Low-Code?What scenarios is low code suitable for?
- ECMAScript 2022 正式发布,有你了解过的吗?
- 测试点等同于测试用例吗
- 扫雷小游戏
- JS中localStorage和sessionStorage
- MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
- String splitting function strtok exercise
- 使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
猜你喜欢
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
面试:简单介绍你参与的一个项目
【CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!)(A~D)】
Pytorch seq2seq 模型架构实现英译法任务
期货开户调整交易所保证金标准
MLX90640 红外热成像仪测温传感器模块开发笔记(十) 成果展示-红眼睛相机
什么是低代码(Low-Code)?低代码适用于哪些场景?
go笔记记录——channel
DOA从一维阵列传感说起
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
随机推荐
攻防世界-web-Training-WWW-Robots
After reshipment tencent greetings to monitor if the corresponding service does not exist by sc. Exe command to add services
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
期货开户交返是行内公开的秘密
uni-app project summary
Redis 相关问题
go笔记记录——channel
[21-Day Learning Challenge] A small summary of sequential search and binary search
Looking back at 5 recessionary times in history: How might this time be different?
JS中对事件代理的理解及其应用场景
JSP how to obtain the path information in the request object?
H5页面打开微信小程序
什么是低代码(Low-Code)?低代码适用于哪些场景?
ICML 2022 || 局部增强图神经网络GNN,在 GCN 和 GAT基础上 平均提高了 3.4% 和 1.6%
Mapped Statements collection does not contain value for的解决方法
鲲鹏编译调试插件实战
AXI4协议介绍
傅立叶变换相关公式
els block boundary deformation processing
冒泡排序函数封装