Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to format datetime in ReactJS

Introduction

Every web application you build will, most likely, deal with dates and times at some point, whether it's for tracking events, scheduling appointments, or even just displaying the current date and time. It's an essential skill for any developer, especially when using a modern framework like ReactJS. But don't worry, handling dates and times in ReactJS isn't rocket science, it's more like cooking. You have some raw ingredients (in this case, date and time data), and you need to follow a recipe (the coding process) to turn those ingredients into a delicious meal (your completed web application). So let's start cooking!

Understanding JavaScript Date Object

Before we dive into formatting dates in ReactJS, it's crucial to understand the JavaScript Date object. This object is a built-in feature of JavaScript, and it allows us to create, read, update and manipulate dates and times. It's like a Swiss army knife for dealing with dates and times.

Here's an example of creating a new date object in JavaScript:

let currentDate = new Date();
console.log(currentDate);

When you run this code, it will print the current date and time to the console. But this isn't very user-friendly, is it? It's like when you first buy ingredients for a recipe - they're in their raw form, and you need to prepare and cook them before they're ready to eat.

ReactJS and Dates

ReactJS is a JavaScript library for building user interfaces, and it's brilliant at doing just that. However, dealing with dates and times isn't something that ReactJS focuses on. It's like a chef who specializes in making the main course but leaves the dessert to someone else.

For formatting dates, we often use external libraries like Moment.js or date-fns. These libraries are like those handy kitchen gadgets that help you chop vegetables or knead dough quickly and easily. They're not essential, but they do make life a lot easier.

Formatting Dates with Moment.js

Moment.js is a popular JavaScript library that simplifies working with dates and times. It's easy to use and offers lots of functionality, like formatting dates, parsing dates, manipulating dates, and much more. Using Moment.js is like having a professional pastry chef in your kitchen, helping you whip up some delicious desserts.

To use Moment.js, you first need to install it:

npm install moment --save

Then, you can import it into your React component:

import moment from 'moment';

Now, let's format the current date. With Moment.js, you can format dates using the format method, and you can specify the format you want the date to be in. Here's an example:

let currentDate = moment().format('MMMM Do YYYY, h:mm:ss a');
console.log(currentDate);

This will output the date in a format like "January 3rd 2022, 3:25:45 pm". It's much more user-friendly, isn't it?

Formatting Dates with date-fns

Date-fns is another popular JavaScript library for working with dates and times. It's like Moment.js's younger sibling - it does many of the same things, but it's a bit smaller and lighter.

To use date-fns, you first need to install it:

npm install date-fns --save

Then, you can import the functions you need from it:

import { format } from 'date-fns';

Date-fns is a bit more modular than Moment.js - instead of importing the whole library, you just import the functions you need. This can help keep your application's size down, which is always a good thing.

Here's how you can format the current date with date-fns:

let currentDate = format(new Date(), 'MMMM do yyyy, h:mm:ss a');
console.log(currentDate);

This will output the same formatted date as the Moment.js example above.

Conclusion

Formatting dates and times in ReactJS is like cooking a meal - it requires some preparation and the right tools, but once you get the hang of it, it's not that difficult. And like cooking, it's a skill that will serve you well in many different situations. So the next time you're working on a ReactJS project and you need to deal with dates and times, don't panic - just remember this guide, and you'll be able to handle it like a pro.