Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2869

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T09:25:10+00:00 2024-11-26T09:25:10+00:00

Day.js | The lightest API to handle dates in JS

  • 61k

Today I am writing again to bring you a library that will help us with the handling of dates in JavaScript, yes, as we all know the handling of dates in JavaScript is not very intuitive.


🤔 Why use dayjs?

Basically the justification for using dayjs is to simplify the handling of dates in JavaScript.

It is a fairly widespread library and which you have probably heard of already, it was presented a while ago as an alternative to Moment which is not recommended for use today, the main reason is the weight and the appearance of new alternatives that offer more modern and lighter solutions, dayjs is an example of this.

Moment

see more here

image

Dayjs

see more here

image

It is very light because it takes advantage of the Treeshaking since the library is fully extensible through plugins that we will add depending on the needs that arise, in this way we will only import the things we need.


🧪 Some examples

Now we will go to see some examples where its use would be justified compared to the native API, either for simplicity, readability or also to prevent possible errors.

We are going to review some of the most interesting functionalities that dayjs offers us.


🧹 Without plugins


Get difference in days between two dates

docs

import dayjs from "dayjs";  dayjs(new Date(2020, 5, 10)).diff(new Date(2020, 5, 1), "day"); // output:  9 
Enter fullscreen mode Exit fullscreen mode


Check if the given date is valid or not

docs

import dayjs from "dayjs";  dayjs("20").isValid(); // output:  false dayjs("2021-09-13").isValid(); // output:  true 
Enter fullscreen mode Exit fullscreen mode


Get the number of days in the month

docs

import dayjs from "dayjs";  dayjs("2021-09-13").daysInMonth() // output: 30 
Enter fullscreen mode Exit fullscreen mode


Add days, months, years, hours, minutes, seconds etc.

docs

import dayjs from "dayjs";  dayjs("2021-09-13 20:09:09").add(20, "minute").format() // output: 2021-09-13T20:29:09+02:00  
Enter fullscreen mode Exit fullscreen mode


Subtract days, months, years, hours, minutes, seconds etc

docs

import dayjs from "dayjs";  dayjs("2021-09-13 20:09:09").subtract(20, "minute").format() // output: 2021-09-13T19:49:09+02:00  
Enter fullscreen mode Exit fullscreen mode


⚡ Extending the functionality through plugins


RelativeTime

docs

Get time difference in string format between current date and given date using Spanish locale

import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; import "dayjs/locale/es";  dayjs.locale("es"); dayjs.extend(relativeTime);  dayjs("2021-09-14T13:28:55.979Z").fromNow(); // example output: en 3 horas 
Enter fullscreen mode Exit fullscreen mode


WeekOfYear

docs

Get week of year

import dayjs from "dayjs"; import weekOfYear from "dayjs/plugin/weekOfYear";  dayjs.extend(weekOfYear);  dayjs("2021-09-13T13:28:55.979Z").week(); // output: 38 
Enter fullscreen mode Exit fullscreen mode


IsSameOrAfter

docs

Check if one date is equal to or greater than another

import dayjs from "dayjs"; import isSameOrAfter from "dayjs/plugin/isSameOrAfter";  dayjs.extend(isSameOrAfter);  // To use `year` granularity pass the second parameter dayjs("2021-09-13").isSameOrAfter("2021-09-14", "year"); // output: true 
Enter fullscreen mode Exit fullscreen mode


MinMax

docs

Get the highest date or the lowest date among the dates of an 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() // output: 2021-09-20T00:00:00+02:00   minDate.format() // output: 2021-09-13T00:00:00+02:00  
Enter fullscreen mode Exit fullscreen mode


IsBetween

docs

Check if the given date is within the indicated date range

import dayjs from "dayjs"; import isBetween from "dayjs/plugin/isBetween";  dayjs.extend(isBetween);  // To use `day` granularity pass the third parameter dayjs("2010-10-21").isBetween(dayjs("2010-10-20"), dayjs("2010-10-25"), "day"); //output: true  // To use `year` granularity pass the third parameter dayjs("2010-10-21").isBetween(dayjs("2010-10-20"), dayjs("2010-10-25"), "year"); //output: false 
Enter fullscreen mode Exit fullscreen mode


AdvancedFormat

docs

Vitamin default formatting options

import dayjs from "dayjs"; import advancedFormat from "dayjs/plugin/advancedFormat";  dayjs.extend(advancedFormat);  dayjs("2021-09-14").format("Q Do k kk X x"); // output: 3 14th 24 24 1631570400 1631570400000 
Enter fullscreen mode Exit fullscreen mode


As can be seen in the examples above, the API is quite simple and readable, it seems to me without a doubt a great option if we need to solve some other complex function with dates in JavaScript.

For view more information go to official dayjs docs.


Thanks for reading me. 😊

thanks

javascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.