Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Day.js handle dates in JavaScript

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how Day.js deals with dates in JavaScript. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Why use day.js

First of all, using day.js can help us deal with dates and times in JavaScript more easily.

You may have heard of many libraries dealing with time in JavaScript, such as Moment, but in 2021, moment.js is not recommended because it is too bulky as a date processing tool. Day.js is a more modern and lightweight library that is easier to expand.

Moment.js

Day.js

It's very lightweight because it can take advantage of TreeShaking and extend through plug-ins, and we can introduce plug-ins according to our own needs, so we end up introducing only what we need.

What would we do without day.js?

In native JavaScript, we want to get the current date like this

Const today = new Date (); const dd = String (today.getDate ()) .padStart (2,'0'); / / Day const mm = String (today.getMonth () + 1) .padStart (2,'0'); / / month const yyyy = today.getFullYear () / / year const curDate = `${yyyy}-${mm}-${dd}` console.log (curDate) / / output: 2021-09-17 this is all we need in day.js. Of course, it also supports many features. Import dayjs from "dayjs"; const curDate = dayjs (). Format ('YYYY-MM-DD'); console.log (curDate) / / output: 2021-09-17Day.js example

Now let's look at some practical and interesting examples that are simpler and more readable than native API.

1. Get the number of days between the two dates import dayjs from "dayjs"; / / the second parameter is specified as' day' represents the daily granularity dayjs (new Date (2021, 10, 1)) .diff (new Date (2021, 9, 17), "day"); / / output: 152. Check whether the date is legal import dayjs from "dayjs"; dayjs ("20"). IsValid (); / / output: falsedayjs ("2021-09-17"). IsValid (); / / output: true3. Get the number of days of the input date month import dayjs from "dayjs"; dayjs ("2021-09-13"). DaysInMonth () / / output: 304. Add day, month, year, hour, minute and second import dayjs from "dayjs"; dayjs ("2021-09-17 08:10:00"). Add (20, "minute"). Format ('YYYY-MM-DD HH:mm:ss') / / output: 2021-09-17 08 add 30005. Minus days, months, years, hours, minutes, seconds

View document

Import dayjs from "dayjs"; dayjs ("2021-09-17 08:10:00"). Subtract (20, "minute"). Format ('YYYY-MM-DD HH:mm:ss') / / output: 2021-09-17 07:50:00 use plug-ins to extend functionality

1. RelativeTime

Gets the time difference between the specified time and now.

Import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; dayjs.extend (relativeTime); dayjs ("2021-09-16 13:28:55"). FromNow (); / / output: 9 hours ago

Here are all the output tables

RangeKeySample Output0 to 44 seconds sa few seconds ago45 to 89 seconds ma minute ago90 seconds to 44 minutes mm2 minutes ago... 44 minutes ago45 to 89 minutes han hour ago90 minutes to 21 hours hh2 hours ago... 21 hours ago22 to 35 hours da day ago36 hours to 25 days dd2 days ago... 25 days ago26 to 45 days Ma month ago46 days to October MM2 months ago... 10 months ago11 months to 17 months ya year ago18 month + yy2 years ago. 20 years ago2. WeekOfYear

Gets the week of the year in which the specified date is

Import dayjs from "dayjs"; import weekOfYear from "dayjs/plugin/weekOfYear"; dayjs.extend (weekOfYear); dayjs ("2021-09-13 14:00:00"). Week (); / / output: 383. IsSameOrAfter

Check whether a date is equal to or greater than a date

Import dayjs from "dayjs"; import isSameOrAfter from "dayjs/plugin/isSameOrAfter"; dayjs.extend (isSameOrAfter); dayjs ("2021-09-17"). IsSameOrAfter ("2021-09-16"); / / output: true4. MinMax

Gets the largest or smallest date in the array

Import dayjs from "dayjs"; import minMax from "dayjs/plugin/minMax" Dayjs.extend (minMax) const maxDate = dayjs.max ([dayjs ("2021-09-13"), dayjs ("2021-09-16"), dayjs ("2021-09-20")]) const minDate = dayjs.min ([dayjs ("2021-09-13"), dayjs ("2021-09-16")) Dayjs ("2021-09-20")] maxDate.format ('YYYY-MM-DD HH:mm:ss') / / output: 2021-09-20 00:00:00minDate.format (' YYYY-MM-DD HH:mm:ss') / / output: 2021-09-13 00 00:00:00minDate.format. IsBetween

Check whether the specified date is within the specified date range

Import dayjs from "dayjs"; import isBetween from "dayjs/plugin/isBetween"; dayjs.extend (isBetween); / / use daily granularity to compare dayjs ("2010-10-21") .isBetween (dayjs ("2010-10-20"), dayjs ("2010-10-25"), "day") / / output: true// uses annual granularity to compare dayjs ("2010-10-21") .isBetween (dayjs ("2010-10-20"), dayjs ("2010-10-25"), "year"); / / output: false the above is how Day.js deals with dates in JavaScript. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report