React Get Current DateTime | Reactjs DateTime Now

React Js Get Current Date Time : In ReactJS, you can retrieve the current datetime in various formats. For ISO 8601 UTC format, use new Date().toISOString();
(2023-07-18T12:36:03Z). To obtain the 24-hour format in UTC, employ new Date().toUTCString();
(2023-07-18 18:04:43). If you prefer the 12-hour format with AM/PM, utilize new Date().toLocaleString('en-US');
(July 18, 2023 at 06:04:05 PM). To get the datetime in RFC 2822 format in UTC, use new Date().toUTCString();
(Tue, 18 Jul 2023 12:33:21 GMT). Lastly, for a custom format, use new Date().toLocaleString('en-US');
(18/07/2023 10:30 AM). ReactJS provides these options to help you manipulate and display react js datetime according to your specific requirements.




Thanks for your feedback!
Your contributions will help us to improve service.
How to Get Current Date and Time in React Js?
The provided code demonstrates how to implement a React component that displays the current date and time. The formatDate
function formats the date object into a string with the desired format. The App
component initializes a state variable currentDate
using the useState
hook and updates it every second using the useEffect
hook and setInterval
.
The rendered output shows the formatted current date and time within a <span>
element. The code will continuously update the displayed date and time in real-time.
React Js Get Current Datetime (18/07/2023 18:06:44)
<script type="text/babel">
const { useEffect, useState } = React;
function formatDate(date) {
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${day}/${month}/${year} ${hours}:${minutes}:${seconds}`;
}
function App() {
const [currentDate, setCurrentDate] = useState(new Date());
useEffect(() => {
const interval = setInterval(() => {
setCurrentDate(new Date());
}, 1000);
return () => {
clearInterval(interval);
};
}, []);
return (
<div className='container'>
<h3>React Js Get Current Date and Time</h3>
<p>Current Date and Time: <span>{formatDate(currentDate)}</span></p>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('app'));
</script>
Output of React Js Get Current Date Time
How can I obtain the current date and time in Reactjs in ISO 8601 format (UTC)?
This Reactjs code snippet fetches and displays the current date and time in the ISO 8601 format (UTC) using Coordinated Universal Time (UTC). It creates a new Date object, converts it to the ISO format, and then formats it with "Z" to represent UTC. The rendered webpage will show the current date and time in the specified format
React Js get Current Date Time - ISO 8601 (2023-07-18T12:36:03ZZ) format (UTC)
<script type="text/babel">
const { useState } = React;
function App() {
const currentDateTime = new Date().toISOString();
const formattedDateTime = new Date(currentDateTime).toISOString().replace(/\.\d{3}/, 'Z');
return (
<div className="container">
<p className="title">React Js Current Date Time (UTC):</p>
<p className="time">{formattedDateTime}</p>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React Js get Current Date Time - ISO 8601 format (UTC)
How can I obtain the current date and time in a 24-hour format (local time) using Reactjs?
This ReactJS code fetches the current date and time in a 24-hour format based on the local time zone. It uses the useState
and useEffect
hooks to update the time every second and display it on the webpage. The resulting time is formatted as "YYYY-MM-DD HH:mm:ss".
The rendered output will show "React Js Date Time Format - 24-hour format (local time)" followed by the current local time in the 24-hour format.
React Js get current Date Time - 24-hour (2023-07-18 18:04:43) format
<script type="text/babel">
const { useState, useEffect } = React;
function App() {
const [currentTime, setCurrentTime] = useState("");
useEffect(() => {
// Update the time every second
const interval = setInterval(() => {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, "0");
const day = String(now.getDate()).padStart(2, "0");
const hours = String(now.getHours()).padStart(2, "0");
const minutes = String(now.getMinutes()).padStart(2, "0");
const seconds = String(now.getSeconds()).padStart(2, "0");
setCurrentTime(
`${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
);
}, 1000);
// Clean up the interval on unmount
return () => clearInterval(interval);
}, []);
return (
<div className="container">
<h3>React Js Date Time Format - 24-hour format (local time)</h3>
<p>Current Local Time (24-hour format): {currentTime}</p>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React Js get current Date Time Format - 24-hour format (local time)
How can I get the current date time in a 12-hour format using Reactjs?
The provided code snippet is a React.js component that displays the current date and time in a 12-hour format with AM/PM indicator, based on the local time zone. It uses the toLocaleString
method to format the date and time according to the specified options. The resulting formatted date and time are rendered within a div element on the webpage.
React Js get current date time : 12-hour (July 18, 2023 at 06:04:05 PM) format
<script type="text/babel">
const { useState } = React;
function App() {
const currentDate = new Date();
const options = {
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: true,
};
const currentDateTimeString = currentDate.toLocaleString(
undefined,
options
);
return (
<div className="container">
<p className="title">
React Js get current date time format : 12-hour format (local
time)
</p>
<p className="time">{currentDateTimeString}</p>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React Js get current date time format : 12-hour format
How can you display the current GMT time in a Reactjs?
This React.js code snippet creates a component that displays the current GMT time in RFC2822 format. It utilizes the useState
and useEffect
hooks. The useState
hook initializes the currentRFC2822Date
state variable. Inside useEffect
, a function getCurrentRFC2822Date
is defined to update the state with the current UTC time in RFC2822 format. This function is called initially and then at one-second intervals using setInterval
. The component renders the GMT time within a <p>
element and continuously updates it in real-time. It offers a simple way to display and refresh the current GMT time in a React application.
React Js Current GMT Time
<script type="text/babel">
const { useState, useEffect } = React;
function App() {
const [currentRFC2822Date, setCurrentRFC2822Date] = useState("");
useEffect(() => {
const getCurrentRFC2822Date = () => {
const currentDate = new Date().toUTCString();
setCurrentRFC2822Date(currentDate);
};
getCurrentRFC2822Date();
const interval = setInterval(getCurrentRFC2822Date, 1000); // Update every second
return () => clearInterval(interval);
}, []);
return (
<div className='container'>
<h2>React Js Current GMT Time</h2>
<p>{currentRFC2822Date}</p>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React Js get current Date Time Format - RFC 2822 format
How can I display the current date and time in the format "18/07/2023 10:30 AM" using Reactjs?
The given Reactjs code displays the current date and time in a shorter format "18/07/2023 10:30 AM". It uses the useState and useEffect hooks to update the date and time, then formats and displays it using a function that converts the time to a 12-hour clock with AM/PM. This format is commonly used in user interfaces for a concise representation of date and time information.
React Js Current Date Time - 18/07/2023 10:30 AM format
xxxxxxxxxx
<script type="text/babel">
const { useState, useEffect } = React;
function App() {
const [currentDate, setCurrentDate] = useState("");
useEffect(() => {
// Get the current date and time
const currentDate = new Date();
// Format the current date and time in the desired short format
const formattedDate = `${currentDate.getDate()}/${
currentDate.getMonth() + 1
}/${currentDate.getFullYear()} ${formatAMPM(currentDate)}`;
// Update the state with the formatted date
setCurrentDate(formattedDate);
}, []);
// Function to format the time in 12-hour format with AM/PM
const formatAMPM = (date) => {
let hours = date.getHours();
let minutes = date.getMinutes();
const ampm = hours >= 12 ? "PM" : "AM";
// Convert hours from 24-hour format to 12-hour format
hours %= 12;
hours = hours || 12;
// Ensure minutes are always displayed with two digits
minutes = minutes < 10 ? `0${minutes}` : minutes;
return `${hours}:${minutes} ${ampm}`;
};
return (
<div className='container'>
<h1>React Js Current Date Time - 18/07/2023 10:30 AM format</h1>
<p>{currentDate}</p>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>