首页 前端知识 Dayjs 的一些常用方法

Dayjs 的一些常用方法

2024-05-10 08:05:59 前端知识 前端哥 679 116 我要收藏

dayjs 的安装和引入

npm install dayjs --save
import dayjs from 'dayjs'
复制

dayjs 获取当前年份 || 月份 || 当前几号的天数  || 星期几 || 几点

// 当前年份
var currentYear = dayjs().year();
// 获取当前月份
var currentMonth = dayjs().month() + 1;
// 返回当前月份有多少天
var currentMonthDay = dayjs('2023-02-06').daysInMonth(); // 28 天
// 获取当前几号的天数 2.8日 返回 Number 8
var currentDay = dayjs().date();
// 获取当前是周几 返回 string 一 || 二 || 三
var currentWeek = dayjs().day();
// 获取当前是几点 13.40 === Number 13
var currentHour = dayjs().hour()
复制

dayjs 设置开始的某个时间 || 结束的某个时间

// year(今年一月1日上午 00:00) || month(本月1日上午 00:00)
// hour(当前时间,0 分、0 秒、0 毫秒) || minute(当前时间,0 秒、0 毫秒)
// week(本周的第一天上午 00:00)
dayjs().startOf('day') // startOf 时间的开始 当天的 00:00:00
dayjs().endOf('day') // endOf 时间的末尾 当天的 23:59:59
// 以下用法详解
dayjs("2023-02-06").startOf('day').format('YYYY-MM-DD HH:mm:ss'), // 转换> 2023-02-06 00:00:00
dayjs("2023-02-06").endOf('day').format('YYYY-MM-DD HH:mm:ss'), // 转换> 2023-02-06 23:59:59
dayjs(formData.dataTime[0]).startOf('day').format('YYYY-MM-DD HH:mm:ss'), // 用法
复制

显示时间戳格式相关

dayjs().format(String)
dayjs('2023-02-06').format('YYYY-MM-DD HH:mm:ss') // 日期 === 年 月 日 时 分 秒 2023-02-06 00:00:00
dayjs().format('YYYY-MM-DD') // 日期 === 年 月 日 2023-02-06
dayjs().format('YYYY-MM') // 日期 === 年 月 2023-02
复制

dayjs (方法)判断某个日期是否 已经在已过日期 ( Boole )

// 方法
dayjs('2023-02-07').isBefore(dayjs()); // 2023-02-06 false ( 2023-02-07 不在 02-06 之前 )
复制

dayjs(方法) 判断某个日期是否跟某个日期相同

dayjs('2023-2-6').isSame(dayjs('2023-2-6')); // true
复制

dayjs(方法) 判断某个日期是否在某个日期之后

dayjs('2023-2-7').isAfter(dayjs('2023-2-9')); // false 如果两个一样那么也是返回 false
复制

时小记,终有成。

转载请注明出处或者链接地址:https://www.qianduange.cn//article/7947.html
标签
评论
还可以输入200
共0条数据,当前/页
发布的文章

JQuery中的load()、$

2024-05-10 08:05:15

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!