- 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 dayjsNow, 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 agoDate 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

สร้างเว็บไซต์ 1 เว็บ ต้องใช้งบเท่าไหร่? เจาะลึกทุกองค์ประกอบ website development cost อยากสร้างเว็บไซต์แต่ไม่มั่นใจในเรื่องของงบประมาณ อ่านสรุปเจาะลึกตั้งแต่ดีไซน์, ฟังก์ชัน และการดูแล พร้อมตัวอย่างงบจริงจาก Till it’s done ที่แผนชัด งบไม่บานปลายแน่นอน
Next.js สอน 14 ขั้นตอนเบื้องต้น: สร้างโปรเจกต์แรกใน 30 นาที เริ่มต้นกับ Next.js ใน 14 ขั้นตอนเพียงแค่ 30 นาที พร้อม SSR/SSG และ API Routes ด้วยตัวอย่างโค้ดง่าย ๆ อ่านต่อเพื่อสร้างโปรเจ็กต์แรกได้ทันทีที่นี่
วิธีสมัคร Apple Developer Account เพื่อนำแอปขึ้น App Store ทีละขั้นตอน อยากปล่อยแอปบน App Store ระดับโลก มาอ่านคู่มือสมัคร Apple Developer Account พร้อมเคล็ดลับ TestFlight และวิธีอัปโหลดที่ง่ายในบทความเดียวนี้ได้เลย
TypeScript Interface คืออะไร? อธิบายพร้อมวิธีใช้และข้อแตกต่างจาก Type เรียนรู้วิธีใช้ TypeScript Interface เพื่อสร้างโครงสร้างข้อมูลที่ปลอดภัยและเข้าใจง่าย พร้อมเปรียบเทียบข้อดีข้อแตกต่างกับ Type ที่คุณต้องรู้ ถูกรวมเอาไว้ในบทความนี้แล้ว
Material-UI (MUI) คืออะไร อยากสร้าง UI สวยงามและเป็นมืออาชีพในเวลาอันรวดเร็วใช่ไหม มาทำความรู้จักกับ Material-UI (MUI) ที่ช่วยให้คุณพัฒนาแอปพลิเคชันบน React ได้ง่ายและดูดีในทุกอุปกรณ์
เปรียบเทียบ 3 วิธีติดตั้ง install node js บน Ubuntu: NVM vs NodeSource vs Official Repo แบบไหนดีที่สุด? เรียนรู้วิธีติดตั้ง Node.js บน Ubuntu ด้วย NVM, NodeSource หรือ Official Repo เลือกวิธีที่เหมาะกับความต้องการของคุณ พร้อมเปรียบเทียบ เพื่อการพัฒนาที่มีประสิทธิภาพ! พูดคุยกับซีอีโอ
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.