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://cdn.jsdelivr.net/npm/@babel/standalone@7.14.6/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel" data-type="module"> const { useState } = React; const App = () => { const [birthDate, setBirthDate] = useState(""); const [years, setYears] = useState(null); const [months, setMonths] = useState(null); const [days, setDays] = useState(null); const calculateAge = (birthDate) => { if (!birthDate) return; const currentDate = new Date(); if (new Date(birthDate) > currentDate) { setBirthDate(""); setYears(null); setMonths(null); setDays(null); alert("Invalid Date of Birth"); return; } const diffTime = currentDate - new Date(birthDate); const totalDays = Math.floor(diffTime / (1000 * 60 * 60 * 24)); setYears(Math.floor(totalDays / 365.25)); setMonths(Math.floor((totalDays % 365.25) / 30.4375)); setDays(Math.floor((totalDays % 365.25) % 30.4375)); }; return ( <div className="container"> <h3>Reactjs Calculate age from given date</h3> <form> <label>Enter your birthdate:</label> <input type="date" value={birthDate} onChange={(e) => { setBirthDate(e.target.value); calculateAge(e.target.value); }} /> </form> {birthDate && ( <p> Your age is {years} years, {months} months, and {days} days </p> )} </div> ); }; ReactDOM.render(<App />, document.getElementById("app")); </script> <style> body { margin: 0; } .container { max-width: 800px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); position: relative; } </style> </body> </html>