One Line of Code to Format Dates in JavaScript (I mean it)
Tags: javascript, beginners, tutorial, typescript
Published: 2022-06-15 06:10:28

I've been using libraries like _Date-fns_ for a long time whenever I need to format dates in JavaScript. But it becomes pretty strange when I try this in tiny applications that require basic date formats like those provided by JavaScript by default.

Most developers, I realized, do this frequently. And I used to believe that this was the best approach until I discovered that we don't always need to utilize libraries to format dates in JavaScript.

If you want to give it a go, here's the code:

![One Line of Code to Format Dates in JavaScript](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e7wjxak37jm2osz10pv.png)

After you've tried it in your own code and confirmed that it works, let's look at why it works and discover some alternative ways to format dates in JavaScript using only one line of code.

![buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozuzikhiuvqynetd5a7z.png)

## Date Formatting in JS

Getting the date in JavaScript is normally not difficult, however formatting these dates to fit your project might be difficult for novices. As a result, the majority of individuals end up utilizing libraries.

The _new Date()_ object is the most often used technique for obtaining the date in JavaScript.

When you call _new Date()_ in your terminal, it defaults to using your browser's time zone and displaying the date as a complete text string, such as **Fri Jul 02 2021 12:44:45 GMT+0100** (British Summer Time).

However, including something like this on your web page or application is unprofessional and difficult to understand. As a result, you are forced to explore for better methods to structure these dates.

Let's look at various methods that work with a date object.

## JavaScript Date Methods

There are several methods that may be applied to the date object. These methods can be used to retrieve information from a date object. Here are a few examples:

- getFullYear() returns the current year as a four-digit value (yyyy)
- getMonth() returns the current month as a number (0-11)
- getDate() returns the current date as a number (1-31)
- getHours() returns the current hour (0-23)
- And much more...

Unfortunately, most of these approaches still need a significant amount of code to convert the dates to the desired format.

For instance, _new Date ().getMonth()_ returns the number 6, which represents **July**. To utilize July in my project, I'll need to write extensive code like this, which can be tedious:

![buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10p7sb4xe4zjasadwnx6.png)

Let's look at two approaches for formatting dates in the best way possible so that you may utilize them for your projects.

## JavaScript's toDateString() method

The _toDateString()_ function in JavaScript returns the date component of a date object as a string in the following format:

- The first three letters in the name of a weekday
- The initial three letters of the month's name
- Two-digit month day, padded on the left by a zero if necessary
- Year (at least four digits), padded on the left with zeros if necessary

![buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l737luim3p0oths3ru9o.png)

One significant disadvantage of this strategy is our inability to modify the date output as we like.

For example, it does not allow us to display dates in our native language. Let's look at another way that, in my opinion, is still one of the greatest.

## The toLocaleDateString() function

In JavaScript, the _toLocaleDateString()_ function returns the date object as a string using local standards. It also accepts parameters as arguments, allowing you/your apps to tailor the function's behavior.

Syntax:

![buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/taagw6arz7bahttnwvs9.png)

Let's look at few cases and their results:


![buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xq6orhtwqyhkdgvn8ukr.png)

You can also choose not to utilize locales or options:


![buy me a coffee sojin samuel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxawk7yu14wmtf3tbb3x.png)

You can also choose to solely utilize locales. Based on my browser's time zone, this will produce the same results as the prior.


![buy me a coffee javascript](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xi5ow1rmssgop8ieud2j.png)

You can also choose to change the choices as you see fit. There are four fundamental options:

- **weekday** — This returns the day of the week in the format you choose (short or long).
- **year** — This function returns the year as a number.
- **month** — This returns the month of the year in the format you choose (short or long).
- **day** – Finally, this produces a number representing the day.

![buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/du03bd04vxswby92ac6e.png)

## Closing and [Buy Me a Coffee](https://www.buymeacoffee.com/sojinsamuel)

There are about seven formatting options available for the date object. Each of these approaches provides a distinct value:

- Fri Jul 02 2021 14:03:54 GMT+0100 is returned by **toString() (British Summer Time)**
- Fri Jul 02 2021 is returned by **toDateString()**.
- 7/2/2021, 2:05:07 PM is returned by **toLocaleString()**.
- **toLocaleDateString()** returns 7/2/2021.
- Fri, 02 Jul 2021 13:06:02 GMT is returned by **toGMTString()**.
- Fri, 02 Jul 2021 13:06:28 GMT is returned by **toUTCString()**.
- **toISOString()** returns the value 2021-07-02T13:06:53.422Z.

If you want more complicated date formats, you will need to build your own. Check out the resources listed below to learn how to generate unique date formats.

## Resources of Use

- [Date in JavaScript: Everything You Need to Know](https://css-tricks.com/everything-you-need-to-know-about-date-in-javascript/)
- [JavaScript - How to Format Date in JavaScript](https://www.tabnine.com/academy/javascript/how-to-format-date/)
- [In JavaScript, how do you format a date?](https://flaviocopes.com/how-to-format-date-javascript/)
- [DateTime Manipulation: The Definitive Guide](https://www.toptal.com/software/definitive-guide-to-datetime-manipulation)

[Be My Twitter Buddy](https://twitter.com/SamuelSojin)


![buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlifem6sfaeelcxfr5h5.png)