Tillitsdone
down Scroll to discover

Parse and Manipulate Dates in Node.js Using Day.js

Learn how to efficiently handle dates in Node.js using Day.js.

This guide covers basic operations, timezone handling, and best practices for working with dates in your applications.
thumbnail

Modern minimalist workspace with scattered calendar pages and warm sunlight streaming through large windows featuring October mist and sand tones shot from above with dramatic lighting high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

How to Parse and Manipulate Dates in Node.js Using Day.js

Working with dates in JavaScript has always been a bit tricky. While the native Date object gets the job done, it often leaves developers wanting more flexibility and convenience. Enter Day.js – a lightweight and modern library that makes date manipulation a breeze in Node.js applications.

Abstract geometric shapes representing time flowing through an hourglass with flowing gradients of navy blue and orange creating a sense of motion captured from a side perspective with strong directional lighting high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Why Choose Day.js?

Day.js has become a popular choice among developers for several compelling reasons. At just 2KB minified and gzipped, it’s incredibly lightweight compared to alternatives like Moment.js. Despite its small size, it provides a powerful API that’s both intuitive and chainable. Plus, its modular architecture means you only import what you need.

Getting Started with Day.js

First, let’s install Day.js in your Node.js project:

Terminal window
npm install dayjs

Now, let’s explore some common use cases and see how Day.js makes date manipulation effortless.

Basic Date Operations

const dayjs = require('dayjs');
// Get current date
const now = dayjs();
console.log(now.format('YYYY-MM-DD HH:mm:ss'));
// Create a specific date
const christmas = dayjs('2024-12-25');
console.log(christmas.format('dddd, MMMM D, YYYY'));

A serene Japanese rock garden with carefully arranged stones and raked sand patterns featuring stone blue and seaweed green colors photographed from a low angle during golden hour high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Advanced Date Manipulation

One of Day.js’s strengths is its intuitive API for adding, subtracting, and comparing dates:

const futureDate = dayjs().add(1, 'month').subtract(2, 'days');
console.log(futureDate.format('MM/DD/YYYY'));
// Check if a date is before another
const isBefore = dayjs('2024-01-01').isBefore('2024-12-31');
console.log('Is before:', isBefore);
// Calculate difference between dates
const diff = dayjs('2024-12-31').diff(dayjs('2024-01-01'), 'days');
console.log('Days difference:', diff);

Working with Timezones

Day.js handles timezones gracefully with the help of plugins:

const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');
dayjs.extend(utc);
dayjs.extend(timezone);
const tokyoTime = dayjs().tz('Asia/Tokyo');
console.log('Tokyo time:', tokyoTime.format());

Best Practices and Tips

  1. Always chain operations for cleaner code
  2. Use appropriate plugins for extended functionality
  3. Format dates consistently throughout your application
  4. Consider locale support for international applications

Remember that Day.js is immutable – operations return new objects instead of modifying existing ones. This helps prevent bugs and makes your code more predictable.

Conclusion

Day.js proves that working with dates doesn’t have to be complicated. Its intuitive API, small footprint, and powerful features make it an excellent choice for Node.js developers who want to handle dates efficiently without the overhead of larger libraries.

A peaceful mountain landscape at sunset with layered rocky peaks featuring maroon and orange hues in the sky captured from a wide-angle perspective with natural lighting high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.