How to use/format dates in ReactJS

I am explaining you, how to use/format dates in ReactJS.
ReactJS is a JavaScript library so you can easily use Javascript code. here my current component name is Unity.
if you want to use/change dates format so you need to install moment package in ReactJS. below I mentioning you package install command through npm.

npm install --save moment

// or

npm install moment

Example 1 :

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

Output :

Sat Oct 15 2022 21:06:14 GMT+0530 (India Standard Time)

Now, I am using Moment library in dates so I can change date format in reactJS.

Example 2 :

import Moment from 'moment';
import "./index.css"
function Unity() {

const getCurrentDate = new Date();
console.log(getCurrentDate);

const getFormatDate = Moment().format('DD-MM-YYYY');

  return (

    <><div><h4>How to format dates in ReactJS</h4>
    <p>{getFormatDate}</p>
   </div></>

  );
}

export default Unity;

Now I am changing date format with date and time. we can use below code –

Example 3 :

const getFormatDate = Moment().format('MMMM Do YYYY, h:mm:ss a');

Output :

October 15th 2022, 9:19:35 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

4 + 1 =