screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script> <script src="https://unpkg.com/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 } = React; function App() { const [dateInputs, setDateInputs] = useState({ startDate: '', endDate: '', }); const [timeDifference, setTimeDifference] = useState({ hours: 0, minutes: 0, }); const calculateTimeDifference = () => { const startTime = new Date(dateInputs.startDate).getTime(); const endTime = new Date(dateInputs.endDate).getTime(); if (isNaN(startTime) || isNaN(endTime)) { alert('Please enter valid start and end dates.'); return; } if (startTime >= endTime) { alert('End date and time must be after start date and time.'); return; } const timeDifference = Math.abs(endTime - startTime); const hours = Math.floor(timeDifference / (1000 * 60 * 60)); const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60)); setTimeDifference({ hours, minutes }); }; const handleInputChange = (event) => { const { name, value } = event.target; setDateInputs({ ...dateInputs, [name]: value, }); }; return ( <div className='container'> <h2>React Js Calculate Time Difference Between Two Dates</h2> <div> <label>Start Date and Time:</label> <input type="datetime-local" name="startDate" value={dateInputs.startDate} onChange={handleInputChange} /> </div> <div> <label>End Date and Time:</label> <input type="datetime-local" name="endDate" value={dateInputs.endDate} onChange={handleInputChange} /> </div> <button onClick={calculateTimeDifference}>Calculate</button> <div> {timeDifference.hours > 0 && ( <p> Time Difference: {timeDifference.hours} hours, {timeDifference.minutes} minutes </p> )} </div> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { text-align: center; padding: 20px; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); margin: 0 auto; max-width: 600px; } h2 { font-size: 24px; margin-bottom: 20px; color: #333; } label { display: block; margin-bottom: 8px; color: #555; } input[type="datetime-local"] { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; color: #333; box-sizing: border-box; } button { background-color: #007bff; color: #fff; border: none; border-radius: 4px; padding: 10px 20px; font-size: 18px; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #0056b3; } p { font-size: 18px; color: #333; margin-top: 10px; } </style> </body> </html>