当前位置:网站首页>Day. JS common methods
Day. JS common methods
2022-04-23 04:56:00 【halo1416】
Preface
Day.js
Instead of modifying localDate.prototype
,Day.js
YesDate
Objects are encapsulated , Just callDayjs()
that will do .- relative
moment.js
nothing more ,day.js
It's smaller , Only 2KB about ;moment.min.js
Yes 16.7KB. - For ordinary projects , There are only a few operations corresponding to time ( format 、 Inquire about 、 Calculation ), These are based on
Day.js
It's enough . alsoDay.js
The documentation is relatively simple 、 Clear !
This article compares with the previous moment.js
The article
Day.js Official documents
Many examples of the current article are processed at the current time , The current time is :2022-04-20
1. Initialization date / Time
dayjs().format('YYYY-MM-DD'); // Initialization date
dayjs().format('YYYY-MM-DD HH:mm:ss'); // Initialization date time
2. Format date / Time
dayjs(value).format('YYYY-MM-DD'); // Initialization date
dayjs(value).format('YYYY-MM-DD HH:mm:ss'); // Initialization date time
3. Add / reduce
dayjs().add / dayjs().subtract
Represents addition and subtraction in the current time ;
dayjs(value).add / dayjs(value).subtract
Represents at a specified time (value) Go up and add and subtract ;
dayjs().add(7, 'day').format('YYYY-MM-DD'); // 2022-04-27 today (2022-04-20) add 7 God
dayjs().add(1, 'month').format('YYYY-MM-DD'); // 2022-05-20 today (2022-04-20) Plus January
dayjs().subtract(2, 'year').format('YYYY-MM-DD'); // 2020-05-20 today (2022-04-20) subtract 2 year
dayjs().subtract(2, 'hour').format('YYYY-MM-DD HH:mm:ss'); // 2022-04-20 14:03:39 Today, now (2022-04-20 16:03:39) subtract 2 Hours
List of all available units :
Company | abbreviation | describe |
---|---|---|
week | w | Zhou |
day | d | week ( Sunday 0, Saturday 6) |
month | M | month (0-11) |
year | y | year |
hour | h | Hours |
minute | m | minute |
second | s | second |
millisecond | ms | millisecond |
4. Get the first or last day of a year or month
Get the first day of a month and a year :
dayjs().startOf('year').format('YYYY-MM-DD HH:mm:ss') // 2022-01-01 00:00:00 => The hours, minutes and seconds formatted on the first day are 0
dayjs().startOf('month').format('YYYY-MM-DD') // 2022-04-01
Get the last day of a month and a year :
dayjs().endOf('year').format('YYYY-MM-DD HH:mm:ss') // 2022-12-31 23:59:59 => Last time The formatted hours, minutes and seconds are 23:59:59
dayjs().endOf('month').format('YYYY-MM-DD') // 2022-04-30
5. Get the day of the week
dayjs().day()
: return 0( Sunday ) To 6( Saturday ) The number of
You can only accept 0-6 The number of :
dayjs().day(6).format('YYYY-MM-DD')
: Get the date of the last Saturday => 2022-04-23
dayjs().day(0).format('YYYY-MM-DD')
: Get the date of the last Sunday => 2022-04-17
6. Get milliseconds
dayjs('2019-01-25').valueOf()
or dayjs().valueOf()
7. Get time difference ( The default output is in milliseconds )
dayjs('2019-01-25').diff('2018-06-05', 'month'); // 7
dayjs('2019-01-25').diff(dayjs('2018-06-05'), 'month'); // 7
List of all available output units :
Company | abbreviation | describe |
---|---|---|
week | w | Zhou |
day | d | week ( Sunday 0, Saturday 6) |
month | M | month (0-11) |
year | y | year |
hour | h | Hours |
minute | m | minute |
second | s | second |
millisecond | ms | millisecond ( Default output unit ) |
8. When getting 、 branch 、 second
current time :2022-04-20 16:55:55 ;
Most of the following methods will overflow forward ; If milliseconds exceed 999, Will last until seconds ; Seconds more than 59, Will last until , The situation here is particularly prominent when setting !
console.log('----- Year of acquisition ', dayjs().year()); // ==>> 2022
console.log('----- Get the month ', dayjs().month()); // 0 To 11 The number of ==>> 3
console.log('----- Get week ', dayjs().day()); // 0( Sunday ) To 6( Saturday ) The number of ==>> 3
console.log('----- Get days ', dayjs().date()); // 1 To 31 The number of ==>> 20
console.log('----- For hours ', dayjs().hour()); // 0 To 23 The number of ==>> 16
console.log('----- Get minutes ', dayjs().minute());// 0 To 59 The number of ==>> 55
console.log('----- Get seconds ', dayjs().second()); // 0 To 59 The number of ==>> 55
console.log('----- Get milliseconds ', dayjs().millisecond()); // 0 To 999 The number of ==>> 333
9. Turn milliseconds into minutes and seconds
// The following milliseconds represent :2022-04-20 17:43:20
const timestr = 1650447800731; // The millisecond value must be number type , If it is string, The result may be different from what you think
console.log(' Turn milliseconds into years - month - Japan when : branch : second ', dayjs(timestr).format('YYYY-MM-DD HH:mm:ss'));
console.log(' Year of acquisition ', dayjs(timestr).year()); //
console.log(' Get the month ', dayjs(timestr).month());
console.log(' Get days ', dayjs(timestr).date());
console.log(' When getting ', dayjs(timestr).hour());
console.log(' Get points ', dayjs(timestr).minute());
Be careful : here year()
、month()
、date()
、hour()
、minute()
、second()
、millisecond()
And other methods can be used
10. Judge whether one date is after another isAfter
// day.js by 2022-04-20
console.log('isAfter', dayjs().isAfter(dayjs('2011-01-01'))) // true
console.log('isAfter', dayjs('2022-04-20').isAfter(dayjs('2022-04-21'))) // false
console.log('isAfter', dayjs('2022-04-20').isAfter(dayjs('2022-04-20'))) // Same as false
11. Judge whether one date is before another isBefore
// day.js by 2022-04-20
console.log('isBefore', dayjs().isBefore(dayjs('2011-01-01'))) // false
console.log('isBefore', dayjs('2022-04-20').isBefore(dayjs('2022-04-21'))) // true
console.log('isBefore', dayjs('2022-04-20').isBefore(dayjs('2022-04-20'))) // The date is also the same when false
12. Judge whether the two dates are the same isSame
// day.js by 2022-04-20
console.log('isSame', dayjs().isSame(dayjs('2011-01-01'))) // false
console.log('isSame', dayjs('2022-04-20').isSame(dayjs('2022-04-21'))) // false
console.log('isSame', dayjs('2022-04-20').isSame(dayjs('2022-04-20'))) // true
13. Judge whether a date is between two dates IsBetween
Be careful :
This feature depends onIsBetween
plug-in unit
How to use... Will also be demonstrated hereDay.js
Plug in for
import dayjs from 'dayjs' // introduce dayjs
import isBetween from 'dayjs/plugin/isBetween' // Introduce related plug-ins
created() {
dayjs.extend(isBetween); // Mount plug-ins
// The use of plug-in
console.log('isBetween', dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25')) )
}
Day.js There are Same or before IsSameOrBefore
and Same or later IsSameOrAfter
Methods , It can be used according to the actual demand , But these two methods need to rely on the corresponding plug-ins !
Be careful :
isAfter、isBefore、isSame、IsBetween The default is to change the date to milliseconds
To compare , So these two methods have a second parameter . That is, specify the granularity of the comparison
console.log('isBefore', dayjs('2022-04-20').isBefore('2015-01-01', 'year'))
List of all available units :
Company | abbreviation | describe |
---|---|---|
date | D | God 00:00 |
day | d | week 00:00 |
month | M | The first day of the month 00:00 |
year | y | 1 month 1 Japan 00 spot |
week | w | First day of the week 00:00 |
isoWeek | Zhou (ISO) | |
hour | h | Hours 00:00:00 |
minute | m | minute 00:00 |
second | s | second 00 |
millisecond | ms | millisecond ( Default comparison unit )0 |
For more detailed usage, see Official documents
This article is only a record of my learning process , For reference only , If there is a problem , Welcome to point out !
版权声明
本文为[halo1416]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230454556464.html
边栏推荐
- Other problems encountered in debugging fingerprints
- Practice and exploration of knowledge map visualization technology in meituan
- Getprop property
- Pixel mobile phone brick rescue tutorial
- Arduino UNO r3+LCD1602+DHT11
- Learning Android from scratch -- baseactivity and activitycollector
- Manually write smart pointer shared_ PTR function
- JS generates a specified number of characters according to some words
- getprop 属性
- Introduction to load balancing
猜你喜欢
Learning Android II from scratch - activity
Learning Android from scratch -- Introduction
持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
Druid -- JDBC tool class case
Introduction to raspberry pie 3B - system installation
Making message board with PHP + MySQL
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
PIP3 installation requests Library - the most complete pit sorting
Sword finger offer: the path with a certain value in the binary tree (backtracking)
Eight misunderstandings that should be avoided in data visualization
随机推荐
Gets all dates between two times
Innovative practice of short video content understanding and generation technology in meituan
Arduino UNO r3+LCD1602+DHT11
泰克示波器DPO3054自校准SPC失败维修
Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
Innovation training (VII) FBV view & CBV view
Detailed explanation of the differences between TCP and UDP
简单的拖拽物体到物品栏
Implementation of switching windows and capturing data in selenium mode
Making message board with PHP + MySQL
COM in wine (2) -- basic code analysis
Record the ThreadPoolExecutor main thread waiting for sub threads
Agile practice | agile indicators to improve group predictability
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
深度学习笔记 —— 微调
getprop 属性
Druid -- JDBC tool class case
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
Leetcode -- heuristic search