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
Hello, Welcome to webdeveloperindia.in. I am a full-stack web developer. Email – [email protected] | Skype – azaruddin23. I have knowledge of PHP, Laravel, Magento 1/2, Codeigniter, WordPress, Joomla, Shopify, Git, Bitbuket, jQuery, Ajax, Javascript and ReactJS.