- Services
- Case Studies
- Technologies
- NextJs development
- Flutter development
- NodeJs development
- ReactJs development
- About
- Contact
- Tools
- Blogs
- FAQ
Format Dates and Times with Day.js in Node.js
Discover practical examples for timezone handling, relative time formatting, and date manipulation techniques.

How to Format Dates and Times with Day.js in Node.js
Working with dates and times in JavaScript has always been a bit tricky. While the native Date object provides basic functionality, it often falls short when we need more advanced date manipulation and formatting. That’s where Day.js comes in – a lightweight alternative to Moment.js that makes handling dates a breeze.
Why Choose Day.js?
Day.js has become increasingly popular among Node.js developers, and for good reason. At just 2KB minified and gzipped, it’s incredibly lightweight while still providing all the essential date-handling features you need. Plus, its chainable API and familiar syntax make it a joy to use.
Getting Started with Day.js
First things first, let’s install Day.js in our Node.js project:
npm install dayjs
Now, let’s import it into our project:
const dayjs = require('dayjs');
Basic Date Formatting
The real power of Day.js lies in its formatting capabilities. Here are some common formatting patterns you’ll find useful:
const now = dayjs();
// Basic formattingconsole.log(now.format('YYYY-MM-DD')); // 2024-11-23console.log(now.format('DD/MM/YYYY')); // 23/11/2024console.log(now.format('MMMM D, YYYY')); // November 23, 2024
Working with Time Zones
One of the most powerful features of Day.js is its timezone handling. First, you’ll need to install the timezone plugin:
const utc = require('dayjs/plugin/utc');const timezone = require('dayjs/plugin/timezone');dayjs.extend(utc);dayjs.extend(timezone);
// Convert between time zonesconst tokyoTime = dayjs().tz('Asia/Tokyo').format('YYYY-MM-DD HH:mm:ss');
Relative Time Formatting
Want to display dates in a more human-readable format? Day.js has got you covered with its relative time feature:
const relativeTime = require('dayjs/plugin/relativeTime');dayjs.extend(relativeTime);
const futureDate = dayjs().add(3, 'days');console.log(futureDate.fromNow()); // in 3 days
const pastDate = dayjs().subtract(5, 'hours');console.log(pastDate.fromNow()); // 5 hours ago
Date Manipulation Made Easy
Day.js makes it simple to perform date calculations:
// Add timeconst tomorrow = dayjs().add(1, 'day');const nextWeek = dayjs().add(1, 'week');
// Subtract timeconst lastMonth = dayjs().subtract(1, 'month');
// Start/end of time periodsconst startOfMonth = dayjs().startOf('month');const endOfYear = dayjs().endOf('year');
Best Practices and Tips
- Always chain your operations for cleaner code
- Use plugins only when needed to keep your bundle size small
- Consider using ISO 8601 format for storing dates
- Remember to handle timezone conversions explicitly
- Use the appropriate date format for your target audience






Talk with CEO
We'll be right here with you every step of the way.
We'll be here, prepared to commence this promising collaboration.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.