Learn to format date in javascript efficiently

In this article, we will see how to format date in javascript efficiently. there are a few ways/methods to format a date in javascript. let's explore them all in this article.

In a developer's life, handling date is an important part of web application development. you might face a situation where you need to format a date and process it. Learn to format date in javascript efficiently.

Why format a date

Most of the time, we can't understand the format of date that is stored in the machine. it's not in human readable format.

For example, consider GitHub issues and pull request.

format date in javascript

you can see the date are Human readable. basically, it is not the format that the dates are stored in database. we need to format to make it human readable.

Let's learn how to format date in javascript with in-built methods and external libraries.

How to format date in javascript

In-built method

Here, we have a simple date function in javascript which returns us a date that is hard to read.

format date in javascript

Let's see some of the in-built date methods to format the date in javascript.

1// In built Method
2const dateToFormat = new Date("August 25 2020")
3
4console.log("javascript date", dateToFormat)
5
6// returns the human readable format
7console.log(dateToFormat.toDateString())

toDateString method returns us the date in human readable format.

format date in javascript

To format with date and time, you can use toString

1// prints whole date and time in readable format
2console.log(dateToFormat.toString())

To display on the time in readable format, you can use

1//prints time from the date in readable format
2console.log(dateToFormat.toTimeString())

To format the date and time in UTC timezone in readable format, you can use

1// prints the date time in UTC timezone
2console.log(dateToFormat.toUTCString())

There are some other methods that can be used to get date/month/year from the date. they are,

1// get the Date from the whole date time. For ex: 1-31
2console.log(dateToFormat.getDate())
3
4// get the day from the whole date time.Ex: 0-6
5console.log(dateToFormat.getDay())
6
7// get the month number from the date time
8console.log(dateToFormat.getMonth())
9
10// get the year from the date time
11console.log(dateToFormat.getFullYear())

format date in javascript

Moment.js

you can use moment js to manipulate date in javascript. this is one of the popular libraries to handle date in javascript applications.

constraint with momentjs is, the size of the package. if you have a simple use-case. it's better to go with in-built methods in javascript.

04

Let's see how to format a date using momentjs

1const moment = require("moment")
2//it formats the date to the way we define.
3console.log(moment().format("YYYY-MM-DD"))

one of the major advantages of using moment is, you can format the date in the structure you want. you can just define different structure of date format.

1const moment = require("moment")
2
3console.log(moment().format("YYYY-MM-DD"))
4
5console.log(moment().format("YYYY-MM"))
6
7console.log(moment().format("MM-DD"))
8
9console.log(moment().format("YYYY-MM-DD HH:mm"))
10
11console.log(moment().format("YYYY-MM-DD hh:mm a"))
  • YYYY - it represents the year in date time
  • MM - represents the month of the specified date
  • DD - represents the date of the date time.
  • HH - defines the Hour in the date time.
  • mm - minutes in the date time.

format date in javascript

date-fns

date-fns is an another popular library to manipulate date in javascript.

1const { format } = require("date-fns")
2
3const dateFormatted = format(new Date(), "yyyy-mm-dd")
4
5console.log(dateFormatted)

date-fns is simple to use and comes in handy to parse and manipulate the date in javascript applications.

Summary

Learning how to manipulate date comes in handy to develop javascript applications. As a developer, it will be helpful to know how to format date in javascript efficiently.

To Read More

How to build an Actionable data ta...

In this article, we will see how to build an Actionable data table using a react...

How to Integrate Google Sheet in No...

This article explains how to Integrated Google sheet with your Nodejs Applicatio...

Kubernetes for Nodejs developers

Do you keep hearing the word kubernetes in the tech community and you couldn't u...