screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.14.0/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.14.0/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <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'> <h3>React Js Current Date Time - 18/07/2023 10:30 AM format</h3> <p>{currentDate}</p> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { margin: 0 auto; width: 500px; text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); padding: 20px; display: flex; flex-direction: column; align-items: center; } </style> </body> </html>