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.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.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 [startDate, setStartDate] = useState(''); const [endDate, setEndDate] = useState(''); const [dateArray, setDateArray] = useState([]); const [startDateError, setStartDateError] = useState(''); const [endDateError, setEndDateError] = useState(''); useEffect(() => { getDates(); }, [startDate, endDate]); const getDates = () => { if (startDate && endDate) { const start = new Date(startDate); const end = new Date(endDate); const days = Math.floor((end - start) / (1000 * 60 * 60 * 24)); const dates = []; for (let i = 0; i <= days; i++) { const currentDate = new Date(start.getTime() + i * 24 * 60 * 60 * 1000); dates.push(currentDate); } setDateArray(dates); } }; const formatDate = (date) => { const day = date.getDate().toString().padStart(2, '0'); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const year = date.getFullYear().toString(); return `${day}/${month}/${year}`; }; const validateInput = () => { const start = new Date(startDate); const end = new Date(endDate); setStartDateError(''); setEndDateError(''); if (start > end) { setStartDateError('Start date must be before end date'); } }; return ( <div className='container'> <h3>React Js Get All Dates Between Two Dates</h3> <div> <label htmlFor="start-date">Start Date:</label> <input type="date" id="start-date" value={startDate} onChange={(e) => setStartDate(e.target.value)} onBlur={validateInput} /> </div> <div> <label htmlFor="end-date">End Date:</label> <input type="date" id="end-date" value={endDate} onChange={(e) => setEndDate(e.target.value)} onBlur={validateInput} /> {startDateError && <div className="error">{startDateError}</div>} {endDateError && <div className="error">{endDateError}</div>} </div> <ul> {dateArray.map((date) => ( <li key={date}>{formatDate(date)}</li> ))} </ul> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } body { font-family: Arial, sans-serif; margin: 0; padding: 0; } .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; width: 600px; } h3 { color: #333; font-size: 24px; margin-bottom: 20px; } label { display: block; margin-bottom: 5px; } input[type="date"] { width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; } .error { color: red; margin-top: 5px; } ul { list-style-type: none; padding: 0; } li { margin-bottom: 5px; } li:before { content: "•"; margin-right: 5px; color: #333; } </style> </body> </html>