Skip to main content

dayjs

Library for parsing, validation, and manipulation of Date objects.

github
View source code

Usage

Abyss bundles the Day.js library, a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for modern browsers with a largely Moment.js-compatible API. We use it internally and encourage you to use it for your own logic as well.

Import Day.js

import { dayjs } from '@uhg-abyss/web/tools/dayjs';

Usage examples

Below are some common examples of how to use Day.js:

// Get the current date and time
const currentDate = dayjs();
// Parse a date from an ISO string
const dateFromIsoString = dayjs('2018-04-04T16:00:00.000Z');
// Parse a date from a Unix timestamp
const dateFromUnix = dayjs(1318781876406);
// Parse a date from a string with a specific format
const dateFromString = dayjs('07/01/2025', 'MM/DD/YYYY');
// Get the day of the month from the current date
const dateInCurrentMonth = dayjs().date();
// Add 7 days to the current date
const sevenDaysFromCurrentDate = dayjs().add(7, 'day');
// Calculate the difference in milliseconds between two dates
const millisecondsBetweenTwoDates = dayjs('2019-01-25').diff(
dayjs('2018-06-05')
);

Installation recommendation

While Abyss provides Day.js and several common plugins out of the box, we recommend installing Day.js separately in your project if you:

  • Need plugins that aren't included in Abyss
  • Want to control your own Day.js version
  • Have specific date/time manipulation requirements

If you only need the basic plugins we provide, you can continue using the Abyss-provided version.

Abyss default plugins

The following Day.js plugins are available in Abyss:

  • isSameOrBefore
  • isSameOrAfter
  • isYesterday
  • isLeapYear
  • isTomorrow
  • isBetween
  • duration
  • toObject
  • isToday
  • customParseFormat

Example usage:

// Check if a date is between two other dates
dayjs('2019-01-25').isBetween('2019-01-01', '2019-02-01');
// Convert to JavaScript `Date` object
dayjs('2019-01-25').toObject();
// Check if it's today
dayjs().isToday();
// Create a duration
dayjs.duration(100);
// Check if a date is between two other dates
dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'), 'year');
// Check if a date is tomorrow
dayjs().add(1, 'day').isTomorrow();
// Check if a date is in a leap year
dayjs('2000-01-01').isLeapYear();
// Check if a date is yesterday
dayjs().add(-1, 'day').isYesterday();
// Check if a date is the same or after another date
dayjs('2010-10-20').isSameOrAfter('2010-10-19', 'year');
// Check if a date is the same or before another date
dayjs('2010-10-20').isSameOrBefore('2010-10-19', 'year');
Table of Contents