dayjs
⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
Top Related Projects
Quick Overview
Day.js is a minimalist JavaScript library for parsing, validating, manipulating, and formatting dates. It aims to be a lightweight alternative to Moment.js with a largely compatible API, making it easy for developers to switch from Moment.js to Day.js.
Pros
- Extremely lightweight (2KB minified and gzipped)
- Immutable and chainable
- Largely compatible with Moment.js API
- Supports plugins for extended functionality
Cons
- Less comprehensive than Moment.js in terms of built-in features
- Smaller community and ecosystem compared to more established date libraries
- May require additional plugins for some advanced functionalities
- Limited built-in localization support (requires plugins)
Code Examples
- Creating and formatting dates:
import dayjs from 'dayjs'
const now = dayjs()
console.log(now.format('YYYY-MM-DD HH:mm:ss'))
// Output: 2023-05-10 15:30:45
- Adding and subtracting time:
const future = dayjs().add(1, 'week').subtract(2, 'days')
console.log(future.format('MMMM D, YYYY'))
// Output: May 15, 2023
- Comparing dates:
const date1 = dayjs('2023-05-10')
const date2 = dayjs('2023-06-15')
console.log(date1.isBefore(date2)) // true
console.log(date1.diff(date2, 'days')) // -36
Getting Started
To use Day.js in your project, follow these steps:
- Install Day.js using npm:
npm install dayjs
- Import and use Day.js in your JavaScript file:
import dayjs from 'dayjs'
const today = dayjs()
console.log(today.format('YYYY-MM-DD'))
- To use plugins, import and extend Day.js:
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
dayjs.extend(relativeTime)
console.log(dayjs().fromNow())
// Output: a few seconds ago
Competitor Comparisons
Parse, validate, manipulate, and display dates in javascript.
Pros of Moment
- More mature and battle-tested library with a longer history
- Extensive localization support for many languages and cultures
- Rich ecosystem of plugins and extensions
Cons of Moment
- Larger bundle size, which can impact performance in web applications
- Mutable API design, which can lead to unexpected side effects
- No longer actively developed, only maintaining for existing projects
Code Comparison
Moment:
moment().format('MMMM Do YYYY, h:mm:ss a');
moment().add(7, 'days');
moment('2010-10-20').isBefore('2010-10-21');
Day.js:
dayjs().format('MMMM D YYYY, h:mm:ss A');
dayjs().add(7, 'day');
dayjs('2010-10-20').isBefore('2010-10-21');
Summary
While Moment has been a popular choice for date and time manipulation in JavaScript, Day.js offers a more lightweight and modern alternative. Day.js provides similar functionality with a smaller footprint and immutable API, making it a preferred option for new projects. Moment, however, still maintains an advantage in terms of extensive localization support and a rich ecosystem of plugins. The syntax between the two libraries is quite similar, allowing for relatively easy migration from Moment to Day.js in many cases.
⏳ Modern JavaScript date utility library ⌛️
Pros of date-fns
- More comprehensive and feature-rich library with a wider range of functions
- Modular architecture allows for tree-shaking and smaller bundle sizes
- Immutable by design, preventing unintended side effects
Cons of date-fns
- Larger overall package size compared to dayjs
- Steeper learning curve due to its extensive API
- May require more configuration for optimal performance
Code Comparison
date-fns:
import { format, addDays } from 'date-fns'
const date = new Date()
const formattedDate = format(date, 'yyyy-MM-dd')
const futureDate = addDays(date, 7)
dayjs:
import dayjs from 'dayjs'
const date = dayjs()
const formattedDate = date.format('YYYY-MM-DD')
const futureDate = date.add(7, 'day')
Both libraries offer similar functionality for basic date operations, but date-fns provides more granular control and a wider range of functions. dayjs has a simpler API and smaller footprint, making it easier to get started with, while date-fns offers more advanced features and better performance optimization options for larger projects.
A tiny (349B) reusable date formatter. Extremely fast!
Pros of tinydate
- Extremely lightweight (340 bytes gzipped) compared to dayjs (2.9KB gzipped)
- Faster performance due to its minimal feature set
- Simple API focused solely on date formatting
Cons of tinydate
- Limited functionality, only supports date formatting
- No parsing capabilities or date manipulation features
- Lacks internationalization support
Code Comparison
tinydate:
import tinydate from 'tinydate';
const stamp = tinydate('{YYYY}-{MM}-{DD}');
console.log(stamp(new Date()));
dayjs:
import dayjs from 'dayjs';
const date = dayjs();
console.log(date.format('YYYY-MM-DD'));
Summary
tinydate is a ultra-lightweight date formatting library, while dayjs is a more comprehensive date manipulation library. tinydate excels in size and performance for simple formatting tasks, but lacks the extensive features and flexibility of dayjs. Choose tinydate for projects where minimal file size is crucial and only basic date formatting is needed. Opt for dayjs when you require a full-featured date library with parsing, manipulation, and internationalization support.
:clock2: Immutable date and time library for javascript
Pros of js-joda
- Immutable date-time objects, preventing unintended side effects
- More comprehensive API for handling time zones and periods
- Closely follows the design of Java 8's java.time package, offering familiarity for Java developers
Cons of js-joda
- Larger bundle size compared to dayjs
- Steeper learning curve for developers not familiar with Java's date-time API
- Less widespread adoption and community support
Code Comparison
js-joda:
const { LocalDate, DateTimeFormatter } = require('@js-joda/core')
const date = LocalDate.parse('2023-05-15')
const formattedDate = date.format(DateTimeFormatter.ofPattern('dd.MM.yyyy'))
dayjs:
const dayjs = require('dayjs')
const date = dayjs('2023-05-15')
const formattedDate = date.format('DD.MM.YYYY')
Both libraries offer similar functionality for parsing and formatting dates. However, js-joda uses a more verbose syntax and separate imports for different components, while dayjs provides a more concise API with a single import. js-joda's approach aligns closely with Java's date-time API, which may be preferable for developers transitioning from Java or working in mixed Java/JavaScript environments.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
English | ç®ä½ä¸æ | æ¥æ¬èª | Português Brasileiro | íêµì´ | Español (España) | Ð ÑÑÑкий | Türkçe | à·à·à¶à·à¶½ | ×¢×ר×ת
Fast 2kB alternative to Moment.js with the same modern API
Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for modern browsers with a largely Moment.js-compatible API. If you use Moment.js, you already know how to use Day.js.
dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss');
- ð Familiar Moment.js API & patterns
- ðª Immutable
- ð¥ Chainable
- ð I18n support
- ð¦ 2kb mini library
- ð« All browsers supported
Getting Started
Documentation
You can find more details, API, and other docs on day.js.org website.
Installation
npm install dayjs --save
API
It's easy to use Day.js APIs to parse, validate, manipulate, and display dates and times.
dayjs('2018-08-08') // parse
dayjs().format('{YYYY} MM-DDTHH:mm:ss SSS [Z] A') // display
dayjs().set('month', 3).month() // get & set
dayjs().add(1, 'year') // manipulate
dayjs().isBefore(dayjs()) // query
ðAPI Reference
I18n
Day.js has great support for internationalization.
But none of them will be included in your build unless you use it.
import 'dayjs/locale/es' // load on demand
dayjs.locale('es') // use Spanish locale globally
dayjs('2018-05-05').locale('zh-cn').format() // use Chinese Simplified locale in a specific instance
Plugin
A plugin is an independent module that can be added to Day.js to extend functionality or add new features.
import advancedFormat from 'dayjs/plugin/advancedFormat' // load on demand
dayjs.extend(advancedFormat) // use plugin
dayjs().format('Q Do k kk X x') // more available formats
ðPlugin List
Usage Trend
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website.
[Become a sponsor via Github] [Become a sponsor via OpenCollective]
Contributors
This project exists thanks to all the people who contribute.
Please give us a ð star ð to support us. Thank you.
And thank you to all our backers! ð
License
Day.js is licensed under a MIT License.
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot