screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/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 [currentTime, setCurrentTime] = useState(''); useEffect(() => { const updateTime = () => { const now = new Date(); const hours = now.getHours(); const minutes = now.getMinutes(); const ampm = hours >= 12 ? 'PM' : 'AM'; const formattedHours = hours % 12 || 12; const formattedMinutes = minutes.toString().padStart(2, '0'); setCurrentTime(`${formattedHours}:${formattedMinutes} ${ampm}`); }; // Update the time every second const intervalId = setInterval(updateTime, 1000); // Clear the interval when the component unmounts return () => clearInterval(intervalId); }, []); return ( <div className='container'> <h2 className='header'>React Get Current Time (12-Hour Format):</h2> <p className='time'>{currentTime}</p> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> .container { align-items: center; margin: 0 auto; padding: 20px; width: 600px; border-radius: 10px; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); } .header { font-size: 24px; color: #333; margin-bottom: 10px; text-align: center; } .time { font-size: 36px; color: #007BFF; font-weight: bold; text-align: center; } </style> </body> </html>