Convert Figma logo to code with AI

iamkun logodayjs

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

46,619
2,278
46,619
994

Top Related Projects

47,953

Parse, validate, manipulate, and display dates in javascript.

34,434

⏳ Modern JavaScript date utility library ⌛️

A tiny (349B) reusable date formatter. Extremely fast!

1,613

:clock2: Immutable date and time library for javascript

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

  1. 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
  1. Adding and subtracting time:
const future = dayjs().add(1, 'week').subtract(2, 'days')
console.log(future.format('MMMM D, YYYY'))
// Output: May 15, 2023
  1. 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:

  1. Install Day.js using npm:
npm install dayjs
  1. Import and use Day.js in your JavaScript file:
import dayjs from 'dayjs'

const today = dayjs()
console.log(today.format('YYYY-MM-DD'))
  1. 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

47,953

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.

34,434

⏳ 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.

1,613

: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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

English | 简体中文 | 日本語 | Português Brasileiro | 한국어 | Español (España) | Русский | Türkçe | සිංහල | עברית

Day.js

Fast 2kB alternative to Moment.js with the same modern API

Gzip Size NPM Version Build Status Codecov License
Sauce Test Status

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

📚Installation Guide

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

📚Internationalization

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]

                                                                                 BestKru          Route Optimizer and Route Planner Software

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.

NPM DownloadsLast 30 Days