DEV Community

Nahid Hasan
Nahid Hasan

Posted on

JS : Do we need Library for simple date formatting? What can you Do ...

We regularly use a date formatting library like moment.js date-fns in our project. I often i see that , i
use it for simple date formatting.

But there is no need to use any library to format date in JavaScript.

Like , for this, 15 - 2 - 2021 you can use

{ new Date(time).getDate() + ' / ' + parseInt(new
Date(time).getMonth() + 1) + ' / ' +
new Date(time).getFullYear()
}

For Am, Pm , like 9 am, You can use this ,

{ new Date().toLocaleString('en-US', { hour: 'numeric', hour12: true }) }

Also what more you can do is, you can make a tiny npm package and share it in your team.

Thank for reading, have a nice day.

Top comments (2)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

The bottom line is

Also what more you can do is, you can make a tiny npm package and share it in your team.

So, we might choose to install a tiny npm package from someone else as well, as a tradeoff.

Same as browser fetch and Node.js http.

Collapse
 
nahidmbstu profile image
Nahid Hasan

yes , but that comes with an security risk , you never know what is in the package other people created.