Checkout the original post https://devaradise.com/date-in-javascript-dayjs-momentjs/ to read with Table of Content
Handling dates and times is a common task in JavaScript development. While JavaScript's native Date object offers a variety of methods to work with dates, there are scenarios where using external libraries like Day.js or Moment.js can simplify your workflow and provide more robust solutions.
In this article, we’ll explore common cases that can be solved by JavaScript’s native Date object and those that are best handled by external libraries. We’ll also compare Day.js and Moment.js to help you choose the right tool for your needs.
The native JavaScript Date object provides a range of methods for working with dates and times. Here are some common use cases where the native Date object is sufficient:
const now = new Date(); // Current date and time const specificDate = new Date('2024-07-16'); // Specific date
const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; // Months are zero-indexed const day = now.getDate(); const hours = now.getHours(); const minutes = now.getMinutes(); const seconds = now.getSeconds();
const now = new Date(); now.setFullYear(2025); now.setMonth(11); // December now.setDate(25); now.setHours(10); now.setMinutes(11); now.setSeconds(12);
const tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1);
const now = new Date(); const dateString = now.toDateString(); // Example: 'Tue Jul 16 2024' const timeString = now.toTimeString(); // Example: '14:12:34 GMT+0200 (Central European Summer Time)'
const parsedDate = new Date(Date.parse('2024-07-16T14:12:34Z'));
const date1 = new Date('2024-07-16'); const date2 = new Date('2024-07-17'); const isSame = date1.getTime() === date2.getTime(); // false const isBefore = date1.getTime() < date2.getTime(); // true
const dayOfWeek = now.getDay(); // 0 for Sunday, 1 for Monday, etc.
const now = new Date(); const isoDate = now.toISOString(); // 2024-07-22T11:30:59.827Z const utcDate = now.toUTCString(); // Mon, 22 Jul 2024 11:30:42 GMT
const date1 = new Date('2024-01-01'); const date2 = new Date('2024-12-31'); const differenceInTime = date2 - date1; const differenceInDays = differenceInTime / (1000 * 3600 * 24); console.log(differenceInDays);
While the native Date object covers basic date manipulation, certain tasks are more efficiently handled by external libraries like Day.js and Moment.js. Here are some scenarios where these libraries excel:
// Day.js const Day.jsDate = Day.js().format('YYYY-MM-DD HH:mm:ss'); // Moment.js const momentDate = moment().format('YYYY-MM-DD HH:mm:ss');
// Day.js Day.js.extend(relativeTime); // require RelativeTime plugin Day.js('2024-01-01').from(Day.js('2023-01-01')); // a year ago // Moment.js moment('2024-01-01').from(moment('2023-01-01')); // a year ago
Day.js.extend(utc); Day.js.extend(timezone); Day.js('2024-07-16 14:12:34').tz('America/New_York'); // Moment.js with moment-timezone moment.tz('2024-07-16 14:12:34', 'America/New_York');
// Day.js Day.js.locale('fr'); const frenchDate = Day.js().format('LLLL'); // dimanche 15 juillet 2012 11:01 // Moment.js moment.locale('fr'); const frenchMomentDate = moment().format('LLLL'); // dimanche 15 juillet 2012 11:01
// Day.js const invalidDay.js = Day.js('invalid date'); if (!invalidDay.js.isValid()) { console.log('Invalid date'); } // Moment.js const invalidMoment = moment('invalid date'); if (!invalidMoment.isValid()) { console.log('Invalid date'); }
// Day.js with customParseFormat plugin Day.js.extend(customParseFormat); const complexDate = Day.js('05/02/69 1:02:03 PM -05:00', 'MM/DD/YY H:mm:ss A Z'); // Moment.js const complexMoment = moment('05/02/69 1:02:03 PM -05:00', 'MM/DD/YY H:mm:ss A Z');
// Day.js Day.js.extend(duration); const durationDay.js = Day.js.duration(5000); // 5 seconds console.log(durationDay.js.asMinutes()); // 0.083333... const humanizedDurationDay.js = Day.js.duration(5000).humanize(); // 'a few seconds' // Moment.js const durationMoment = moment.duration(5000); // 5 seconds console.log(durationMoment.asMinutes()); // 0.083333... const humanizedDurationMoment = moment.duration(5000).humanize(); // 'a few seconds'
Day.js and Moment.js are two popular libraries for date manipulation in JavaScript. While both serve the same purpose, they have distinct differences in terms of performance, features, and usability. This comparison will help you decide which library is better suited for your project.
Feature | Day.js | Moment.js | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
~2KB | ~70KB | |||||||||||||||||||||||||||||||||
Immutability |
Yes | No | |||||||||||||||||||||||||||||||||
Plugin Architecture |
Yes | Limited | |||||||||||||||||||||||||||||||||
Feature Set |
Basic (extensible via plugins) | Comprehensive | |||||||||||||||||||||||||||||||||
|
Limited (via plugins) | Extensive (Moment Timezone) | |||||||||||||||||||||||||||||||||
Ecosystem |
Growing | Mature | |||||||||||||||||||||||||||||||||
Documentation | Good | Extensive | |||||||||||||||||||||||||||||||||
Modern Syntax |
Yes | No | |||||||||||||||||||||||||||||||||
API Compatibility |
Similar to Moment.js | N/A | |||||||||||||||||||||||||||||||||
Status | Actively maintained | Deprecated |
When working with dates in JavaScript, it’s essential to choose the right tool for the task. Native JavaScript Date objects are sufficient for basic date manipulation tasks, but for more advanced operations, libraries like Day.js and Moment.js offer powerful and convenient features.
When deciding between Day.js and Moment.js, consider the specific needs of your project.
The above is the detailed content of Working with Date in Javascript: new Date() vs Day.js vs Moment.js. For more information, please follow other related articles on the PHP Chinese website!